文章目录
- 1 门面模式,Facade Pattern
- 2 适配器模式, Adapter Pattern
- 3 原型模式, Adapter Pattern
- 4 单例模式,Singleton Pattern
- 5 多例模式,Multition Pattern
- 6 策略模式,Strategy Pattern
- 7 代理模式,Proxy Pattern
- 8 工厂模式,Factory Methond Pattern
- 9 抽象工厂模式,Abstract Factory Pattern
- 10 门面模式,Facade Pattern
- 11 Adapter Pattern
- 12 模版模式,Template Method Pattern
- 13 建造者模式,Builder Pattern
- 14 桥梁模式,Bridge Pattern
- 15 命令模式,Command Pattern
- 16 装饰模式,Decorator Pattern
- 17 迭代器模式,Iterator Pattern
- 18 组合模式,Composite Pattern
- 19 观察者模式,Observer Pattern
- 20 责任链模式,Chain of Responsibility Pattern
- 21 访问者模式,Visitor Pattern
- 22 状态模式,State Patter
- 23 原型模式,Prototype Pattern
- 24 中介者模式,Mediator Pattern
- 25 解释器模式,Interpreter Pattern
- 26 亨元模式,Flyweight Pattern
- 27 备忘录模式,Memento Pattern
软件设计过程中,该选择那种设计模式,除了借鉴直接的经验,还可以从以下方面考虑:
- 领域对象模型中,类之间的关系是什么(继承、实现、关联、依赖、组合、聚合)
- 容易变化的部分是什么,即易于扩展的部分
- 不容易变化的部分是什么,即需要复用的部分
按照我们的思路,设计模式之所以多种多样,就是对应的领域模型中上面三个方面有很大不同。把握住以上几点,就能正确地选用设计模式
门面模式,Facade Pattern
- 门面类和被门面类封装的类之间是继承或者依赖关系
- 类的内部接口、内部逻辑经常变化;不被需要的对外接口容易变化;
- 类的对外部接口不容易变
适配器模式, Adapter Pattern
- 适配器类和被适配的类之间是继承或者依赖关系
- 需要适配的类,或者其接口容易变化
- 适配的类的接口不容易变化
原型模式, Adapter Pattern
- 不同类之间有共同的父类
- 类的部分属性、方法容易变化
- 类的另外一部分属性、方法不容易变化,且构建复杂,成本很高
单例模式,Singleton Pattern
- 类在全局唯一存在,其他类与单例类是
- 类的唯一性不会变化
多例模式,Multition Pattern
- 多例类的对象之间是聚合关系
- 多例类的对象的数量容易变化
策略模式,Strategy Pattern
- 对象和算法,是依赖关系
- 逻辑框架不容易变化
- 策略算法容易变化
代理模式,Proxy Pattern
- 代理和被代理的类之间是依赖关系
- 代理者的接口不容易变化
- 被代理者的接口具体实现容易变化
工厂模式,Factory Methond Pattern
- 工厂模式产生的对象一般有相同的父类(继承关系)
- 由工厂产生对象不会变
- 工厂产生对象所属的类的种类是不断变化(增多或减少)
抽象工厂模式,Abstract Factory Pattern
- 同工厂模式
- 变化的是,工厂创建对象所属的类的接口会不断变化
门面模式,Facade Pattern
- 封装类和被封装的类之间是依赖关系
- 接口类(封装类)的接口不容易变化
- 实现类(被封装的类)的接口容易变化
Adapter Pattern
- 同门面模式
- 适配器类更倾向于兼容现有系统的接口需求
模版模式,Template Method Pattern
建造者模式,Builder Pattern
- 构建、配置、注入依赖容易变化,且较复杂
桥梁模式,Bridge Pattern
命令模式,Command Pattern
装饰模式,Decorator Pattern
迭代器模式,Iterator Pattern
组合模式,Composite Pattern
观察者模式,Observer Pattern
- 观察者和被观察者是关联关系
- 被观察者的数量或种类容易变化