Code will be shared at https://github.com/NeilRen/DesignPatterns
Original This article is original, author: Ren Fei. Please cite the author and source when reposting.
The term “design pattern” did not originally appear in software design, but in architecture. In 1977, the renowned American architect and director of the Center for Environmental Structure at UC Berkeley, Christopher Alexander, described common architectural design problems in his book A Pattern Language: Towns, Buildings, Construction and proposed 253 basic patterns for designing towns, neighborhoods, houses, gardens, and rooms. In 1987, Kent Beck and Ward Cunningham first applied Christopher Alexander’s pattern ideas to the generation of graphical user interfaces in Smalltalk, but it did not draw the attention of the software community.
It was not until 1990 that the software engineering community began discussing design patterns, and later held multiple seminars on the topic. In 1995, four authors — Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides — co-published the book Design Patterns: Elements of Reusable Object-Oriented Software, which collects 23 design patterns. This was a milestone event in the design pattern field and led to a breakthrough in software design patterns. These four authors are also known in software development by their anonymous name, the “Gang of Four” (GoF).
GoF’s 23 Design Patterns
Divided by what kind of work the pattern is used to accomplish, this approach falls into three categories: creational patterns, structural patterns, and behavioral patterns.
- Creational patterns: used to describe “how to create objects,” with the main characteristic of “separating object creation from use.” GoF provides 5 creational patterns: Singleton, Prototype, Factory Method, Abstract Factory, and Builder.
- Structural patterns: used to describe how to compose classes or objects into a larger structure according to some layout. GoF provides 7 structural patterns: Proxy, Adapter, Bridge, Decorator, Facade, Flyweight, and Composite.
- Behavioral patterns: used to describe how classes or objects collaborate to accomplish tasks that a single object cannot complete alone, and how to assign responsibilities. GoF provides 11 behavioral patterns: Template Method, Strategy, Command, Chain of Responsibility, State, Observer, Mediator, Iterator, Visitor, Memento, and Interpreter.
| Scope\Purpose | Creational | Structural | Behavioral |
|---|---|---|---|
| Class pattern | Factory Method | (Class) Adapter | Template Method, Interpreter |
| Object pattern | Singleton, Prototype, Abstract Factory, Builder | Proxy, (Object) Adapter, Bridge, Decorator, Facade, Flyweight, Composite | Strategy, Command, Chain of Responsibility, State, Observer, Mediator, Iterator, Visitor, Memento |
Functions of GoF’s 23 Design Patterns
- Singleton pattern: a class can only generate one instance; the class provides a global access point for external code to obtain that instance. Its extension is the limited-multiton pattern.
- Prototype pattern: take an object as a prototype and clone multiple new instances similar to the prototype by copying it.
- Factory Method pattern: define an interface for creating products; subclasses decide what product to produce.
- Abstract Factory pattern: provide an interface for creating a product family; each of its subclasses can produce a series of related products.
- Builder pattern: decompose a complex object into several relatively simple parts, create them separately according to different needs, and finally assemble them into the complex object.
- Proxy pattern: provide a proxy for an object to control access to it. That is, the client accesses the object indirectly through the proxy, thereby restricting, enhancing, or modifying some of its characteristics.
- Adapter pattern: convert a class’s interface into another interface the client expects, so that classes that could not work together due to incompatible interfaces can work together.
- Bridge pattern: separate abstraction from implementation so they can vary independently. It uses composition instead of inheritance, thereby reducing the coupling between the two variable dimensions of abstraction and implementation.
- Decorator pattern: dynamically add some responsibilities to an object, i.e., add extra functionality to it.
- Facade pattern: provide a consistent interface for multiple complex subsystems, making those subsystems easier to access.
- Flyweight pattern: use sharing techniques to effectively support the reuse of a large number of fine-grained objects.
- Composite pattern: compose objects into a tree hierarchy so that users have consistent access to both individual objects and composite objects.
- Template Method pattern: define the skeleton of an algorithm’s operations, deferring some steps to subclasses, so that subclasses can redefine certain steps of the algorithm without changing its structure.
- Strategy pattern: define a family of algorithms, encapsulate each one, and make them interchangeable; changes to an algorithm do not affect the client using it.
- Command pattern: encapsulate a request as an object, separating the responsibility of issuing the request from the responsibility of executing it.
- Chain of Responsibility pattern: pass a request from one object in the chain to the next until the request is handled. This removes the coupling between objects.
- State pattern: allow an object to change its behavior when its internal state changes.
- Observer pattern: a one-to-many relationship exists among multiple objects; when one object changes, it notifies the other objects of the change, thereby affecting their behavior.
- Mediator pattern: define a mediator object to simplify the interaction between original objects, reducing the coupling among objects in the system so the original objects need not know each other.
- Iterator pattern: provide a way to sequentially access a series of data in an aggregate object without exposing its internal representation.
- Visitor pattern: without changing the collection elements, provide multiple ways to access each element in a collection — that is, each element can be visited by multiple visitor objects.
- Memento pattern: capture and save an object’s internal state without violating encapsulation, so it can be restored later.
- Interpreter pattern: provide how to define a language’s grammar and how to interpret sentences of the language — the interpreter.
