GOF 23种设计模式

1. 创建型模式

1.1 单例模式 Singleton Pattern

最简单的设计模式之一, 属于创建型模式. 它提供了一种创建对象的最佳方式. 这个模式涉及到一个单一的类, 该类负责创建自己的对象, 同时确保只有单个对象被创建. 这个类提供了一种访问其唯一的对象的方式, 可以直接访问, 不需要实例化该类的对象.

Ensure a class has only one instance, and provide a global point of access to it.

1.2 工厂方法模式 Factory Method Pattern

定义一个用于创建对象的接口, 让子类决定实例化哪一个类. 工厂方法是一个类的实例化延迟到子类.

Define an interface for creating an object, but let subclass decide whick class to instantiate. Factory Method lets a class defer instantiation to subclass.

1.3 抽象工厂模式 Abstract Factory Pattern

为创建一组相关或者相互依赖的对象提供一个接口, 而且无需指定他们的具体类.

Provide an interfacefor creating families of related or dependent objects without specifying their concrete classes.

1.4 创建者模式 Builder Pattern

将一个复杂对象的构建与它的表示分离, 使得同样的构建过程可以创建不同的表示.

也叫建造者模式, 生成器模式.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

1.5 原型模式 Prototype Pattern

用原型示例指定创建对象的种类, 并且通过拷贝这些原型创建新的对象.

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

2. 行为型模式

2.1 迭代器模式 Iterator Pattern

提供一种方法访问一个容器对象中各个元素, 而不暴露该对象的内部细节.

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

2.2 命令模式 Command Pattern

一种数据驱动的设计模式. 请求以命令的形式包裹在对象中, 并传给调用对象. 调用对象寻找可以处理该命令的对象, 并把该命令传给相应的对象, 该对象执行命令.

Encapsulate a request as an object, thereby letting you parameterize clients with different request, queue or log request, and support undoable operations.

2.3 解释器模式 Interpreter Pattern

提供了评估语言的语法或表达式的方式。这种模式实现了一个表达式接口,该接口解释一个特定的上下文。它被用在SQL解析、符号处理引擎等。

Given a language, define a representation for its grammer with an interpreter that uses the representation to interpret that uses the representation to interpret sentences in the language.

2.4 责任链模式 Chain of Responsibility Pattern

使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理为止。

Avoid coupling the sender of a request to its receiver by giving more than one object a change to handle the request. Chain the receiving object and pass the request along the chain until an object handles it.

2.5 观察者模式 Observer Pattern

定义对象间一种一对多的依赖关系, 使得每当一个对象改变状态, 所有依赖于它的对象都会得到通知并被自动更新.

Define a one-to-many dependency between objects so that when an object change state, all its dependents are notified and updated automatically.

2.6 中介者模式 Mediator Pattern

用一个中介对象封装一系列对象(同事)的交互, 中介者使各对象不需要显示的相互作用, 从而使其耦合松散, 而且中介者可以独立地改变他们之间的交互.

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicity, and it lets you vary their interaction independently.

2.7 备忘录模式 Memento Pattern

在不破坏封装性的前体下, 捕获一个对象的内部状态, 并在该对象之外保存这个状态. 这样以后就可将对象恢复到原先保存的状态.

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

2.8 状态模式 State Pattern

允许一个对象内在状态发生改变时改变行为, 使得这个对象看起来像改变了类型. 状态模式的核心是封装, 状态的变更引起行为的变动, 从外部看来就好像该对象对应的类发生改变一样.

Allow an object to alert its behavior when its internal state changes. The object will appear to change its class.

2.9 策略模式 Strategy Pattern

定义一组算法, 将每个算法都封装起来, 并且他们之间可以相互切换. 类似于"条条大路通罗马".

Define a family of algorithms, encapsulate each one, and make them interchangeable.

2.10 模板方式模式 Template Method Pattern

定义一个操作汇总的算法的骨架, 然后将一些步骤延迟到子类中. 使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤.

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

2.11 访问者模式 Visitor Pattern

封装一些作用于某些数据结构中的各元素的操作, 它可以在不改变数据结构的前提下定义作用于这些元素的新的操作.

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

3. 结构型模式

3.1 适配器模式 Adapter Pattern

将一个类的接口变换成客户端所期待的另一种接口, 从而使原本因接口不匹配而无法在一起工作的几个类能够在一起工作.

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interface.

3.2 组合模式 Composite Pattern

将对象组合成树形结构以表示"部分-整体"的层次结构, 使得用户对单个对象和组合对象的使用具有一致性.

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual object and compositions of objects uniformly.

3.3 代理模式 Proxy Pattern

为其他对象提供一种代理以控制对这个对象的访问.

Provide a surrogate or placeholder for another object to control access to it.

3.4 桥梁模式 Bridge Pattern

将抽象和实现解耦, 使得两者可以独立的变化.

Decouple an abstraction from its implementation so that two can vary independently.

3.5 装饰模式 Decorator Pattern

动态地给一个对象添加一些额外的职责, 就增加功能来说, 它相比生成子类更加灵活.

Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexiable alternative to subclass for extending functionality.

3.6 门面模式 Facade Pattern

要求一个子系统的外部与其他内部的通信必须通过一个统一的对象进行. 门面模式提供高层次的接口, 使得子系统更易于使用.

Provide a unified interface to a set of interfaces in a subsystem. Facede defines a higher-level interface that makes the subsystem easier to use.

3.7 享元模式 Flyweight Pattern

使用共享对象可以有效地支持大量的细力度的对象.

Use sharing to support large numbers of fine-grained objects efficiently.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章