Summary of Design Patterns 02–Structural Patterns
1 Adapter mode
1.1 Design intent
Convert a set of interfaces to another desired interface, and turn incompatible interfaces into compatible
1.2 Solve the problem
Solve the problem of interface compatibility
1.3 Advantages and disadvantages
advantage
Combining unrelated classes, providing class reuse, and high flexibility
Disadvantage
- Design Principles:
- Subcategory expansion:
- Design level:
- Performance aspects:
- The code is readable: calls between unrelated classes are prone to semantic transformation, which is difficult to understand.
- Scenario limitations:
2 Bridge mode
2.1 Design intent
Separate abstraction and implementation,
2.2 Solve the problem
In situations where there are many possible changes, using inheritance will cause class explosion problems, and it is not flexible to expand. If inheritance is used, the number of classes will be an explosion of M multiplied by N.
2.3 Advantages and disadvantages
advantage
Separation of abstraction and implementation, free combination, strong expansion capability, and high flexibility
Disadvantage
- Design Principles:
- Subcategory expansion:
- Design level: higher requirements for high-level abstraction
- Performance aspects:
- The code is readable:
- Scenario limitations:
3 Appearance mode
3.1 Design intent
Provide a consistent interface for a group of interfaces in the subsystem; the appearance mode defines a high-level interface, which integrates the interfaces of the subsystem to make it simple and easy to use.
3.2 Solve the problem
Reduce the complexity of the subsystem interface.
3.3 Advantages and disadvantages
advantage
Satisfy minimum knowledge, high security, high flexibility
Disadvantage
- Design principle: the subsystem is changed, and the appearance interface needs to be changed; the open and close principle is not satisfied
- Subcategory expansion:
- Design level:
- Performance aspects:
- The code is readable:
- Scenario limitations:
4 Proxy mode
4.1 Design intent
Provide a proxy for other objects to control access to this object.
4.2 Solve the problem
Hide the business and technical details of direct access;
4.3 Advantages and disadvantages
advantage
Single responsibility, high scalability, intelligence, hidden details
Disadvantage
- Design Principles:
- Subcategory expansion:
- Design level:
- Performance: too many agents affect performance
- The code is readable:
- Scenario limitations:
5 Combination mode
5.1 Design intent
Combine objects into a tree structure to represent a "part-whole" hierarchical structure. The combined mode enables users to have consistency in the use of single objects and combined objects.
5.2 Solve the problem
The tree structure blurs the concepts of simple elements and complex elements. The client program can process complex elements like simple elements, thereby decoupling the client program from the internal structure of complex elements.
5.3 Advantages and disadvantages
advantage
Simple to use, free to increase nodes
Disadvantage
- Design principle: The realization class aggregates the realization class and does not conform to the dependency inversion.
- Subcategory expansion:
- Design level:
- Performance aspects:
- The code is readable:
- Scenario limitations:
6 Decoration mode
6.1 Design intent
Dynamically add additional functions to an object, which is more flexible than extending subclasses
6.2 Solve the problem
Extending the subclass will cause the expansion of the subclass, and it is impossible to dynamically add or remove functions during runtime
6.3 Advantages and disadvantages
advantage
The decoration and the decorated object are mutually decoupled and highly flexible
Disadvantage
- Design Principles:
- Subcategory expansion:
- Design level:
- Performance aspects:
- Code is readable: multiple nesting, high complexity
- Scenario limitations:
7 Flyweight mode
7.1 Design intent
Use sharing technology to effectively support the sharing of a large number of fine-grained objects.
7.2 Solve the problem
Solve the problem of multiple copies of a large number of fine-grained objects, causing memory consumption
7.3 Advantages and disadvantages
advantage
Reduce object creation, reduce the number of copies, and improve memory efficiency
Disadvantage
- Design Principles:
- Subcategory expansion:
- Design level: In reality, it is often impossible for an object to be absolutely shareable. It is necessary to separate the shareable and non-shareable parts, and the design requirements are high.
- Performance aspects:
- The code is readable:
- Scenario limitations: