There are two ways to classify design patterns, namely, according to the purpose of the pattern and according to the scope of the pattern
1. Classification according to purpose
According to what the purpose of the pattern is to accomplish, this pattern can be divided into three types: creation pattern, structural pattern and behavior pattern
- The creation pattern describes how objects are created. Its main feature is the separation of object creation and use. GoF provides 5 creation patterns, such as singleton, prototype, factory mode, Abstract factory and Builder.
- Structural patterns: Used to describe how a type or object is arranged in a certain way into a larger result. GoF provides seven structural patterns: proxy, adapter, bridge, decoration, appearance, share element, composition.
- Behavioral pattern: Describes how classes or objects work together to accomplish tasks that a single object could not accomplish alone, and how responsibilities are assigned. Template methods are provided in GoF:
Policy, naming, chain of responsibility, state, observer, mediator, iterator, visitor, memo, interpreter
And 11 behavioral patterns.
2. Classification according to scope of action
- Class pattern: Used to deal with relationships between classes and subclasses, which are established by inheritance and are static and determined at compile time. Factory methods, (class) adapters, template methods, and interpreters in GoF belong to this pattern.
- Object pattern: Used to deal with relationships between objects, which can be implemented through composition and aggregation, that can change at run time and are more dynamic. All but four of the above are object modes in GoF.
Classification of 23 design patterns
3. GoF features 23 design modes
- Singleton pattern: Only one instance of a class can be generated. This class provides a global access point for external access to the instance. The extension is the finite-multiple pattern.
- Prototype pattern: Clone an object as a Prototype by copying it to create a new instance similar to the Prototype.
- Factory Method: Defines an interface for creating products, and subclasses decide what products to produce.
- Abstract Factory: Provides an interface to create a family of products, each subclass of which can generate a series of related products.
- Builder pattern: Decompose a complex object into relatively simple parts, then create them separately according to different needs, and finally create the complex object.
- Proxy mode: Provides a Proxy for an object to control access to that object. That is, a client indirectly accesses an object through a proxy to restrict, enhance, or modify some of its features.
- Adapter pattern: Converts the interface of one class into another interface that the customer expects, making classes that cannot work together due to interface incompatibility work together.
- Bridge pattern: Separate the abstraction from the implementation so that they can be transformed independently. It is realized by using combinatorial relation instead of inheritance relation, thus reducing the coupling degree of abstraction and realization of these two variable dimensions.
- Decorator pattern: Dynamically add some responsibility to an object, that is, add additional functionality.
- Facade pattern: Provides a consistent interface to multiple complex subsystems, making them more accessible.
- Flyweight pattern: Sharing techniques are used to efficiently support reuse of large numbers of fine-grained objects.
- Composite pattern: Groups objects into a tree-like hierarchy, giving users consistent access to individual and Composite objects.
- Pattern method pattern: Define the skeleton of an algorithm in an operation and defer some steps of the algorithm to a subclass so that the subclass can redefine specific steps of the algorithm without changing the structure of the algorithm.
- Strategy pattern: A series of algorithms are defined, and each algorithm is encapsulated so that they can be replaced with each other, and algorithm changes do not affect the customers using the algorithm.
- Command pattern: Encapsulates a request as an object, separating the responsibility for making the request from the responsibility for executing the request.
- Chain of Responsibility pattern: Pass requests from one object in the Chain to the next until the request is responded to. In this way, objects are decoupled.
- State mode: Allows an object to change its behavior when its internal State changes.
- The Observer pattern: There is a one-to-many relationship between objects. When one object changes, the change is notified to the other objects, thereby affecting the behavior of the other objects.
- Mediator mode: Define an intermediary object to simplify the interaction between the original objects, reduce the degree of coupling between the objects in the system, so that the original objects do not need to understand each other.
- The Iterator pattern: Provides a way to access a series of data in an aggregate object sequentially without exposing the internal representation of the aggregate object.
- Visitor pattern: Provides multiple access to each element in a collection without changing the collection elements, that is, multiple Visitor objects for each element.
- Memento pattern: Retrieves and saves the internal state of an object so that it can be restored later without breaking encapsulation.
- The Interpreter pattern: Provides the grammar of how to define a language and the method of interpreting language sentences, called the Interpreter.
Source: GoF’s classification and functionality of 23 design patterns