The 23 GOF design modes are generally divided into three categories: Creation Mode, structure mode, and behavior mode.
The creation mode includes: 1. FactoryMethod (Factory method mode); 2. Abstract Factory (Abstract Factory mode); 3. Singleton (Singleton mode); 4. Builder mode ); 5. Prototype (Prototype ).
Structural modes include: 6. Bridge (Bridge Mode); 7. Adapter (Adapter mode); 8. Decorator (decoration mode); 9. Composite (combination mode ); 10. Flyweight (metadata mode); 11. Facade (appearance mode); 12. Proxy mode ).
Behavior patterns include: 13. TemplateMethod (template method mode); 14. Strategy (Policy mode); 15. State (State mode); 16. Observer (Observer mode ); 17. Memento (memorandum mode); 18. Mediator (intermediary mode); 19. Command (Command mode); 20. Visitor (Visitor mode ); 21. Chain of Responsibility (responsible Chain mode); 22. Iterator (Iterator mode); 23. Interpreter (Interpreter mode ).
Factory Method: Defines an interface for creating objects, so that the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to its subclass.
Abstract Factory: Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
Singleton: Ensure that a class has only one instance and provides a global access point to it.
Builder: Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Prototype: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
Bridge: Separates abstract parts from their implementations so that they can all change independently.
Adapter: Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
Decorator: Dynamically add some additional responsibilities to an object. In terms of extended functions, the Decorator mode is more flexible than the subclass generation method.
Composite: Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the customer's use of a single object and Composite object consistent.
Flyweight: Use the sharing technology to effectively support a large number of fine-grained objects.
Facade: Provides a consistent interface for a group of interfaces in the subsystem. The Facade mode defines a high-level interface, which makes the subsystem easier to use.
Proxy: Provides a proxy for other objects to control access to this object.
Template Method: Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The Template Method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
Strategy: Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode makes the algorithm changes independent of the customers who use it.
State: Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.
Observer: Defines a one-to-many dependency between objects, so that when the status of an object changes, all objects dependent on it will be notified and automatically refreshed.
Memento: Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the Saved state.
Mediator: Encapsulate a series of object interactions with an intermediary object. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
Command: Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support cancelable operations.
Visitor: Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
Chain of Responsibility: To remove the coupling between the request sender and receiver, multiple objects have the opportunity to process the request. Connect these objects into a chain and pass the request along the chain until an object processes it.
Iterator: Provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.
Interpreter: For a given language, define a representation of its syntax and define an interpreter that uses this representation to interpret sentences in the language.
Singleton: (1) Intention: ensure that a class has only one instance and provide a global access point to it; (2) Applicability: when a class can only have one instance and the customer can access it from a well-known access point, when this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code; (3) Advantages: A. controlled access to A unique instance: Because the Singleton class encapsulates its unique instance, therefore, it can strictly control how and when customers access it. B. narrow down the namespace: The Singleton mode is an improvement for global variables, it avoids the global variables that store unique instances from polluting namespace. C. Allows refinement of operations and representations: The Singleton class can have subclasses, it is easy to configure an application with this extension class instance. You can use the instance of the class you need to configure the application at runtime. D. Allow variable target instances: this mode makes it easy for you to change your mind and allow multiple instances of the Singleton class. In addition, you can use the same method to control the number of instances used by the application. Only the operations that allow access to the Singleton instance need to be changed; E. more flexible than class operations: another way to encapsulate the Singleton function is to use class operations (static member functions in C ++ or class methods in Smalltalk ). However, the two language technologies cannot change the design to allow a class to have multiple instances. In addition, static member functions in C ++ are not virtual functions, so subclasses cannot be redefined with polymorphism.
Sample Code 1:
# Include
# Include
# Include
Using namespace std; class Singleton {private: Singleton () {} static Singleton * single; public: static Singleton * GetInstance () {if (single = NULL) {single = new Singleton ();} return single ;}}; Singleton * Singleton: single = NULL; // static variable class out-of-class initialization // client int main () {Singleton * s1 = Singleton: GetInstance (); Singleton * s2 = Singleton: GetInstance (); if (s1 = s2) cout <"OK" <
<<"no"<
Sample Code 2:
Singleton. h:
# Ifndef _ SINGLETON_H _ # define _ SINGLETON_H _ # include
Using namespace std; class Singleton {public: static Singleton * Instance (); protected: Singleton (); // Singleton cannot be instantiated, therefore, declare its constructor as protected or directly declare it as privateprivate: static Singleton * _ instance;}; # endif //~ _ SINGLETON_H _
Singleton. cpp:
# Include "Singleton. h" # include
Using namespace std; Singleton * Singleton: _ instance = 0; Singleton: Singleton () {cout <"Singleton..." <
Main. cpp:
#include "Singleton.h"#include
using namespace std;int main(){Singleton* sgn = Singleton::Instance();/*result:Singleton ...*/return 0;}
Singleton mode structure:
References:
1. Big talk Design Model C ++
2. Design Patterns-Analysis of GoF23 Design Patterns
3. Design Patterns-Basics of reusable Object-Oriented Software
<<"no"<