# Design Patterns

**设计模式**（design pattern）是对软件设计中普遍存在（反复出现）的各种问题，所提出的解决方案。

有四位在软件开发领域很有名的人出版了一本书《设计模式：可复用面向对象软件的基础》，一共收录了 23 种设计模式。这四个人常称为 GoF（Gang of Four）。23 种设计模式分类：

* 创建型
  * [简单工厂](https://yunzhao.gitbook.io/notes/computer-science/creational/factory#jian-dan-gong-chang)（不属于 GoF）
  * [工厂方法](https://yunzhao.gitbook.io/notes/computer-science/creational/factory#gong-chang-fang-fa)
  * [抽象工厂](https://yunzhao.gitbook.io/notes/computer-science/creational/factory#chou-xiang-gong-chang)
  * [生成器](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/creational/builder)
  * [原型](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/creational/prototype)
  * [单例](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/creational/singleton)
* 结构型
  * [适配器](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/adapter)
  * [桥接](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/bridge)
  * [组合](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/composite)
  * [装饰](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/decorator)
  * [外观](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/facade)
  * [享元](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/flyweight)
  * [代理](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/structural/proxy)
* 行为型
  * [责任链](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/chain-of-responsibility)
  * 命令
  * 解释器
  * [迭代器](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/iterator)
  * 中介者
  * 备忘录
  * [观察者](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/observer)
  * [状态](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/state)
  * [策略](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/strategy)
  * [模板方法](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/behavioral/template-method)，并与 [Callback](https://yunzhao.gitbook.io/notes/computer-science/behavioral/template-method#callback) 做了比较。
  * 访问者

{% hint style="info" %}

* 创建型解决对象创建问题，将创建和使用代码解耦。
* 结构型解决类或对象的组合或组装问题，将不同功能的代码解耦。
* 行为型解决类或对象的交互问题，将不同行为的代码解耦。
  {% endhint %}

学习设计模式需要先了解[ UML](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/uml) 和一些[基本原则](https://yunzhao.gitbook.io/notes/computer-science/design-patterns/principle)。

当然除了 GoF 收录的 23 种设计模式，还有许多其它设计模式。比如[并发领域的一些设计模式](https://yunzhao.gitbook.io/notes/java/concurrency/concurrency-design-patterns)。

学习设计模式有很好好处：

* 应对面试
* 告别烂代码
* 提高复杂代码的设计和开发能力
* 读源码、学框架事半功倍
* 为职场发展做铺垫

平时我们评价代码质量的好坏有很多词语，我们最常用的标准有：

* 可维护性（maintainability）
* 可读性（readability）
* 可扩展性（extensibility）
* 灵活性（flexibility）
* 简洁性（simplicity）
* 可复用性（reusability）
* 可测试性（testability）

**面向对象**由于具有丰富的特性，所以是设计原则、设计模式的编码实现基础。\
**设计原则**是知道我们代码设计的一些经验总结。\
**设计模式**是针对软件开发中经常遇到的一些设计问题，总结出来的一套解决方案或者设计思路。比设计原则更加具体、可执行。\
**编程规范**主要解决的是代码的可读性问题。\
**重构**作为保持代码质量不下降的有效手段，利用的就是面向对象、设计原则、设计模式、编码规范这些理论。

![](https://3232244687-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LYZow-MmROshIrkwdtE%2F-Lth7O6-pa4-Eom4H75C%2F-LtrbgTRLAeiWsIq3EbH%2Fimage.png?alt=media\&token=342d0fc4-1c0c-46df-8094-6e8944e775e4)
