1. Creation mode (creational pattern)
The Factory mode (Factory method) is a common pattern. The application of Factory mode is clear and the design idea is simple. From using polymorphism to just one static method, there are many variations in the factory pattern. I'm used to simply using the Factory mode, which is the Factory mode with only static methods. The following Factory mode code is simple and clean:
Myfactory.getclassinstance (). Dofunction ();
The class factory does not carry the business logic, and the impact of demand changes on the plant is usually very small. So using heavy-weight factory models is often not a good deal. A set of heavyweight factory classes that contain hierarchical relationships can mean over-design.
The singleton mode (Singleton Method) is closely related to the factory model. From the implementation point of view, the singleton mode is a special case of the factory pattern, but the two patterns of the application scenarios are different, so they belong to different patterns.
Abstract Factory mode (FactoryMethod) is the promotion of Factory mode. Abstract Factory mode application scenarios are more specific and rigorous. In a design that uses abstract factories, the abstract factory model may collapse if the different product families evolve in the future. In the actual application, the different product family each evolution, finally parted the situation is some, the user proposed such demand really lets the person "shocking". Before using the abstract factory model, it is important to ensure that these product families are used in a consistent manner from now to in the future.
The construction mode can be obtained by changing the Factory mode slightly. The "machining process" of the factory model is hidden , while the "processing process" of the construction mode is exposed . This is different, making the construction model more flexible while also losing grace.
2. Template mode and Policy mode (strategy method)
Template patterns and policy patterns are similar in application scenarios, but are implemented in different ways, using inheritance and the latter using delegates .
template mode is probably one of the most "old" patterns, and in the early days of using object-oriented technology, "inheritance" is a great way to go, and many designers may unconsciously use template patterns. The disadvantage of template mode is that the concrete implementation and the general algorithm are tightly coupled, so that the concrete implementation can only be manipulated by a general algorithm. However, in an inheritance relationship, the information of the parent class can be exposed more to subclasses, and this subtle communication (contrary to object-oriented design principles) is more flexible and convenient in some specific applications.
The policy pattern is the classic use of delegates . The strategy mode eliminates the coupling between the general algorithm and the concrete implementation, so that the concrete implementation can be manipulated by several general algorithms. The policy model also adds class hierarchies , which are more complex than template patterns.
Template patterns and policy patterns can often be replaced with each other. They are like test papers, template mode is the blank question , the strategy mode is the choice problem .
3. Model for simplifying problems
The façade mode (Façade method) hides a complex set of interfaces behind a simple and specific interface.
The Mediator mode (mediator method) wraps the reference relationship between objects in a specific container.
The combined mode (Composite method) describes the structural relationship of the whole and the part, and allows the structure to be handled in a consistent manner.
The above models have played a part in simplifying the problem to the user.
4. Extended function mode
Both the visitor mode (Visitor method) and the adornment mode (Decorator method) can dynamically add functionality without altering the existing class structure.
The visitor pattern assigns an object on an existing class structure to a class called a visitor, configures the object, alters the object, or extends functionality in the appropriate method of the visitor.
decoration mode " injects " an object from an existing class structure into an ornamental class, extending its functionality in the adornment class.
The visitor pattern and the adornment mode are different in actual effect. The visitor pattern can be used to assign objects to the corresponding methods, thus processing or expanding each object separately . The decorative mode can only be used in a consistent manner to all the decorative objects are processed or expanded , in order to achieve different processing or expansion, can only add new decoration class.
Too many "adornment classes" may cause the business logic to disperse and complicate the program structure. For each specific derived class, the "Access class" must have a corresponding method, adding the derived class should also increase the way to access the class. The need for extended functionality is frequent, and it is worthwhile to consider the use of the above pattern.
5. Other common patterns
Bridge Mode (Bridges method). Class is a container that encapsulates behavior and attributes, whereas a class's set of behaviors can evolve independently, and the most straightforward idea is to use inheritance to encapsulate different behaviors in separate subclasses. Bridge mode solves this problem from a different angle. The bridge mode encapsulates the independent evolution behavior in another class system, and evolves independently from the original class system, and the two-class system is the "use" relationship at the level of abstraction. In many OO textbooks, the shape class encapsulates attributes and draw methods, and in bridge mode, "shapes" and "brushes" are two groups of independent evolutionary classes, at the level of abstraction, "shape" paints itself with "brushes".
Adapter Mode (Adapter method) is a common pattern, it is relatively simple, and sometimes used in conjunction with other modes.
Command mode is known by Martin as "one of the simplest and most elegant modes". The charm of the command pattern is that it has the same skills for each class "training", and that the "training" class "Flex" is stronger and can produce incredible abilities.
6. Less-needed patterns
Viewer Mode (Observer method). Both Java and C # Implement the Observer pattern.
Iterative sub-mode (Iterator method). In the Java and C # languages, you can use aggregation classes instead.
Memo Mode (Memento method). Can be replaced with the serialization capability of class.
Responsibility Chain Mode (Chain of Responsibilitymethod). Can be substituted in other ways, such as the observer pattern, the message mechanism provided by the language itself, and so on.
The interpreter mode (Interpreter method). Personally think that it is an algorithm, not a pattern.
Agent Mode (proxy method). If you know from the beginning that certain underlying policies are bound to be replaced, it is still necessary to use proxies to isolate these policies. Otherwise, there is little need to use it.