Six principles of design patterns
I. six principles of the design model
Name |
Explanation |
0. Single Responsibility Principle (SRP) |
For a class, there should be only one reason for its change. |
I. "open-closed" principle (OCP) |
In the software design pattern, this kind of principle can not be modified, but can be expanded is also the most important design principle. That is, software entities (classes, templates, functions, and so on) should be extensible, but cannot be modified. [Popularity]: during design, we always think about it and try to make this class good enough. If you have finished writing it, Don't modify it. If you want to add new classes, we will be done, the original code remains unchanged. |
Ii. Li's replacement principle (LSP) |
1. If a software object uses a parent class, it must be applicable to the Child class and cannot detect the difference between the parent class object and the Child class object. That is to say, in the software, replace the parent class with its Child class, and the behavior of the program remains unchanged. [One sentence ]:Child types must be able to replace their parent types. |
Iii. Dependency inversion principle (DIP) |
1. The high-level module should not depend on the underlying module. Both of them should depend on abstraction. 2. abstraction should not depend on details, but on abstraction ( [Vernacular]: For interface programming, do not implement programming. |
Iv. Interface isolation principle (ISP) |
1. It is better to use multiple special interfaces than to use a single total interface. In other words, from the perspective of a customer class: the dependence of a class on another class should be based on the minimum interface. 2. too bloated interfaces are contaminated with interfaces. Customers should not be forced to rely on methods they do not need. |
5. Principles of synthesis/aggregation multiplexing (CARP) |
Try to use synthesis/aggregation, and try not to use class inheritance. [Aggregation]: indicates a weak relationship of ownership. Object A can contain object B, but object B is not part of object. [Synthesis]: a strong relationship of ownership. The strict relationship between the part and the whole is presented, and the relationship is consistent with the overall life cycle. |
Vi. dumit rules) Minimum knowledge Principle |
Emphasize the loose coupling between classes. That is, if the two classes do not need to communicate with each other directly, then the two classes should not send a direct interaction. If one class needs to call a method of another class, it can be forwarded by a third party. |
___ Most of the content is taken from 《Big talk Design Model"
1. The landlord? Isn't the question 6 design patterns? How can I list seven?
A: different books are not the same. The single mode principle and the interface isolation principle are mentioned in most cases. This document lists and provides detailed analysis after further exploration.
2. Is the interface isolation principle different from the single responsibility principle?
A: Wrong. The interface isolation principle is different from that of a single responsibility.
A single responsibility requires a single responsibility for classes and interfaces, and focuses on duties. This is a logical division of the business;
The interface isolation principle requires that the interface methods should be as few as possible. For example, the responsibility of an interface may include 10 methods. These 10 methods are put in one interface and provided to multiple modules for access. Each module can access the interface according to the specified permissions, outside the system, the document restricts "do not use methods to access", which is allowed according to the principle of single responsibility and is not allowed according to the principle of interface isolation, because it requires "try to use multiple special interfaces", what does a special interface mean? It means that each module should be provided with a single interface, and several interfaces should be provided to several modules, rather than creating a large and bloated interface to accommodate access from all clients.
Six principles of design patterns