README.md 4.8 KB
Newer Older
L
liu-jianhao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# C++设计模式

## 什么是设计模式
“每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复劳动”。
——Christopher Alexander

## 如何解决复杂性?
+ 分解
  + 人们面对复杂性有一个常见的做法:即分而治之,将大问题分解为多个小问题,将复杂问题分解为多个简单问题。
+ 抽象
  + 更高层次来讲,人们处理复杂性有一个通用的技术,即抽象。由于不能掌握全部的复杂对象,我们选择忽视它的非本质细节,而去处理泛化和理想化了的对象模型。
  
  
## 面向对象设计原则
1. 依赖倒置原则(DIP)
  + 高层模块(稳定)不应该依赖于低层模块(变化),二者都应该依赖于抽象(稳定) 。
  + 抽象(稳定)不应该依赖于实现细节(变化) ,实现细节应该依赖于抽象(稳定)。
2. 开放封闭原则(OCP)
  + 对扩展开放,对更改封闭。
  + 类模块应该是可扩展的,但是不可修改。
3. 单一职责原则(SRP)
  + 一个类应该仅有一个引起它变化的原因。
  + 变化的方向隐含着类的责任。
4. Liskov 替换原则(LSP)
  + 子类必须能够替换它们的基类(IS-A)。
  + 继承表达类型抽象。
5. 接口隔离原则(ISP)
  + 不应该强迫客户程序依赖它们不用的方法。
  + 接口应该小而完备。
6. 优先使用对象组合,而不是类继承
  + 类继承通常为“白箱复用”,对象组合通常为“黑箱复用” 。
  + 继承在某种程度上破坏了封装性,子类父类耦合度高。
  + 而对象组合则只要求被组合的对象具有良好定义的接口,耦合度低。
7. 封装变化点
  + 使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另一侧产生不良的影响,从而实现层次间的松耦合。
8. 针对接口编程,而不是针对实现编程
  + 不将变量类型声明为某个特定的具体类,而是声明为某个接口。
  + 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口。
  + 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案。
L
liu-jianhao 已提交
40 41 42

## 从封装变化角度对模式分类
### 组件协作:
L
liu-jianhao 已提交
43
+ [Template Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Template%20Method)
L
liu-jianhao 已提交
44
+ [Observer/Event](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Observer)
L
liu-jianhao 已提交
45
+ [Strategy](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy)
L
liu-jianhao 已提交
46
+ [Strategy(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy-Pattern)
L
liu-jianhao 已提交
47
### 单一职责:
L
liu-jianhao 已提交
48
+ [Decorator](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Decorator)
L
liu-jianhao 已提交
49
+ [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge)
L
liu-jianhao 已提交
50
### 对象创建:
L
liu-jianhao 已提交
51
+ [Factory Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Factory%20Method)
L
liu-jianhao 已提交
52
+ [Abstract Factory](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Abstract%20Factory)
L
liu-jianhao 已提交
53
+ [Prototype](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Prototype)
L
liu-jianhao 已提交
54
+ [Builder](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Builder)
L
liu-jianhao 已提交
55
### 对象性能:
L
liu-jianhao 已提交
56
+ [Singleton](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Singleton)
L
liu-jianhao 已提交
57
+ [Flyweight(享元模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Flyweight)
L
liu-jianhao 已提交
58
### 接口隔离:
L
liu-jianhao 已提交
59
+ [Façade(门面模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Facade)
L
liu-jianhao 已提交
60
+ [Proxy](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Proxy)
L
liu-jianhao 已提交
61
+ [Mediator(中介者)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Mediator)
L
liu-jianhao 已提交
62
+ [Adapter](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Adapter)
L
liu-jianhao 已提交
63
### 状态变化:
L
liu-jianhao 已提交
64
+ [Memento(备忘录)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Memento)
L
liu-jianhao 已提交
65
+ [State](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/State)
L
liu-jianhao 已提交
66
### 数据结构:
L
liu-jianhao 已提交
67
+ [Composite(组合模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Composite)
L
liu-jianhao 已提交
68
+ [Iterator](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Iterator)
L
liu-jianhao 已提交
69
+ [Chain of Resposibility(职责链)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Chain%20of%20Resposibility)
L
liu-jianhao 已提交
70
### 行为变化:
L
liu-jianhao 已提交
71
+ [Command](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Command)
L
liu-jianhao 已提交
72
+ [Visitor](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Visitor)
L
liu-jianhao 已提交
73
### 领域问题:
L
liu-jianhao 已提交
74
+ [Interpreter](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Interpreter)
L
liu-jianhao 已提交
75 76 77 78 79 80 81 82 83 84 85 86


## 总结
### 现代较少用的模式
+ Builder
+ Mediator
+ Memento
+ Iterator
+ Chain of Resposibility
+ Command
+ Visitor
+ Interpreter