This is the 8th day of my participation in the August Text Challenge.More challenges in August
Design patterns
Represents best practices commonly adopted by experienced object-oriented software developers. Design pattern is a solution to the common problems faced by software developers during software development. These solutions have been developed by numerous software developers over a long period of trial and error.Copy the code
What is the GOF (Gang of Four)?
In 1994, Four authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, published a book called Design Patterns – Elements of Reusable Object-oriented Software (DESIGN Patterns – Reusable Object-oriented Software Elements) is the first book to introduce the concept of design patterns in Software development.
The authors are collectively known as GOF (Gang of Four). The design patterns they propose are based on the following object-oriented design principles.
- Program interfaces rather than implementations.
- Prefer object composition to inheritance.
classification
There are two classification methods, namely, according to the purpose of the pattern and according to the scope of the role of the pattern.Copy the code
Divide according to purpose
Patterns can be divided into creation patterns, structural patterns and behavioral patterns according to what they are used to accomplish.
- The creation pattern describes how objects are created. Its main feature is the separation of object creation and use. GoF provides five creation patterns: singleton, prototype, factory method, Abstract factory, and Builder.
- Structural patterns: Used to describe how classes or objects are arranged into a larger structure. GoF provides seven structural patterns: proxy, adapter, bridge, decoration, facade, share, and composition.
- Behavioral patterns: Describe how classes or objects work together to accomplish tasks that a single object could not accomplish alone, and how responsibilities are assigned. GoF provides 11 behavioral patterns, including template methods, policies, commands, chains of responsibility, states, observers, mediators, iterators, visitors, memos, and interpreters.
According to the scope of action
Patterns can be divided into class patterns and object patterns according to whether they are mainly used on classes or objects.
- 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 or aggregation, that can change at run time and be more dynamic. All but four of the above are object modes in GoF.
Design pattern principles
The open closed principle
Open for extensions, closed for modifications.Copy the code
role
- Impact on software testing
If the software follows the open closed principle, only the extended code needs to be tested during software testing, because the original test code still works.
- You can improve your code’s reusability
The smaller the granularity, the more likely it is to be reused; In object-oriented programming, programming according to atoms and abstractions can improve code reusability.
- Can improve software maintainability
Software that follows the open and closed principle is more stable and continuous, making it easier to expand and maintain.
Richter’s substitution principle
Inheritance must ensure that the properties held by the superclass are still true in the subclassesCopy the code
role
- Richter’s substitution principle is one of the important ways to realize open – close principle.
- It overcomes the disadvantage of poor reusability caused by overwriting the parent class in inheritance.
- It is the guarantee of the correctness of the action. That is, class extensions do not introduce new errors into existing systems, reducing the likelihood of code errors.
- Enhance the robustness of the program, while changing can be very good compatibility, improve the maintenance of the program, scalability, reduce the risk introduced when the requirements change.
Rely on the inversion principle
A high-level module should not depend on a low-level module; both should depend on its abstraction; Abstractions should not depend on details, details should depend on abstractions. The core idea is: program to the interface, not to the implementation.Copy the code
role
- The dependency inversion principle can reduce the coupling between classes.
- The dependence inversion principle can improve the stability of the system.
- The dependency inversion principle can reduce the risks associated with parallel development.
- The dependency inversion principle improves code readability and maintainability.
Interface Isolation Principle
A client should not be forced to rely on methods it does not use. There is another definition of this principle: the dependency of one class on another should be based on the smallest interfaceCopy the code
advantages
- Splitting a bloated interface into multiple small-grained interfaces can prevent the proliferation of external changes and improve the flexibility and maintainability of the system.
- Interface isolation improves system cohesion, reduces external interactions, and reduces system coupling.
- If the interface granularity is properly defined, the system stability can be guaranteed. However, if the definition is too small, it will lead to too many interfaces and complicate the design. If the definition is too large, flexibility is reduced and customization services cannot be provided, bringing unpredictable risks to the overall project.
- The use of multiple specialized interfaces also provides a hierarchy of objects, since the overall interface can be defined through interface inheritance.
- Reduces code redundancy in project engineering. A large interface that is too large often places a lot of unnecessary methods inside it, forcing redundant code to be designed when implementing the interface.
Demeter’s Rule (least Know rule)
If two software entities do not have to communicate directly, then direct calls to each other should not occur and can be forwarded by a third party. Its purpose is to reduce the degree of coupling between classes and improve the relative independence of modulesCopy the code
advantages
- The coupling degree between classes is reduced and the relative independence of modules is improved.
- As the affinity degree is reduced, the class reusability and system scalability are improved.
Principle of composite reuse
In software reuse, we should first use association relation such as combination or aggregation, and then consider using inheritance relation.Copy the code
advantages
- It maintains class encapsulation. Because the internal details of component objects are invisible to the new object, this reuse is also known as “black box” reuse.
- Poor coupling between old and new classes. This reuse requires fewer dependencies, and the only way a new object can access a component object is through its interface.
- High flexibility of reuse. This reuse can occur dynamically at run time, with new objects dynamically referencing objects of the same type as component objects.
Single responsibility
The single responsibility principle states that a class should have only one cause for its change, otherwise the class should be split. The core of the single responsibility principle is to control the granularity of classes, decouple objects, and improve their cohesionCopy the code
advantages
- Reduce class complexity. The logic of a class having one responsibility is certainly much simpler than having multiple responsibilities.
- Improve the readability of classes. As complexity decreases, readability increases.
- Improve system maintainability. Improved readability makes it easier to maintain.
- Risk reduction due to change. Change is inevitable, and if the single responsibility principle is followed well, when you modify one function, you can significantly reduce the impact on other functions.