设计模式的类别
创建型设计模式:专注于处理对象创建机制(通过控制创建过程来减少创建对象时导致的项目复杂性)。包括:构造函数、工厂、抽象、原型、单例和生成器。
结构型设计模式:关注对象的组合以及确定不同对象之间关系的简单方法(系统的一部分发生变化时,整个结构不受影响;将系统中不适合的部分重铸为符合需求的部分)。包括:装饰器、外观(Facade)、享元(Flyweight)、适配器和代理。
行为型设计模式:聚焦于改进或简化不同对象之间的通行。包括:迭代器、中介者、观察者和访问者。
设计模式分类
GoF提及的23种设计模式:
| ****Creational**** | Based on the concept of creating an object. |
|---|---|
| Class | |
| Factory Method | This makes an instance of several derived classes based on interfaced data or events. |
| Object | |
| Abstract Factory | Creates an instance of several families of classes without detailing concrete classes. |
| Builder | Separates object construction from its representation, always creates the same type of object. |
| Prototype | A fully initialized instance used for copying or cloning. |
| Singleton | A class with only a single instance with global access points. |
| ****Structural**** | Based on the idea of building blocks of objects. |
|---|---|
| Class | |
| Adapter | Match interfaces of different classes therefore classes can work together despite incompatible interfaces. |
| Object | |
| Adapter | Match interfaces of different classes therefore classes can work together despite incompatible interfaces. |
| Bridge | Separates an object's interface from its implementation so the two can vary independently. |
| Composite | A structure of simple and composite objects which makes the total object more than just the sum of its parts. |
| Decorator | Dynamically add alternate processing to objects. |
| Facade | A single class that hides the complexity of an entire subsystem. |
| Flyweight | A fine-grained instance used for efficient sharing of information that is contained elsewhere. |
| Proxy | A place holder object representing the true object. |
| ****Behavioral**** | Based on the way objects play and work together. |
|---|---|
| Class | |
| Interpreter | A way to include language elements in an application to match the grammar of the intended language. |
| Template Method | Creates the shell of an algorithm in a method, then defer the exact steps to a subclass. |
| Object | |
| Chain of Responsibility | A way of passing a request between a chain of objects to find the object that can handle the request. |
| Command | Encapsulate a command request as an object to enable, logging and/or queuing of requests, and provides error-handling for unhandled requests. |
| Iterator | Sequentially access the elements of a collection without knowing the inner workings of the collection. |
| Mediator | Defines simplified communication between classes to prevent a group of classes from referring explicitly to each other. |
| Memento | Capture an object's internal state to be able to restore it later. |
| Observer | A way of notifying change to a number of classes to ensure consistency between the classes. |
| State | Alter an object's behavior when its state changes. |
| Strategy | Encapsulates an algorithm inside a class separating the selection from the implementation. |
| Visitor | Adds a new operation to a class without changing the class. |