Introduction to the six principles of object-oriented design and six major object-oriented design
In fact, they all complement each other. That is to say, some principles must be used to implement themselves.
The six principles are as follows:
1) A single responsibility principle and a reasonable class should have only one reason for its change, that is, a single responsibility, that is, there should be only one designed class function;
Advantage: eliminate coupling and reduce code Rigidity caused by demand changes.
2) The Open-Close principle is that the design should have good support for expansion, while the modification should be strictly limited.
Advantage: the coupling between various parts of the program is reduced, and its adaptability, flexibility, and stability are both good. When new functions need to be added to an existing software system, you do not need to modify the abstraction layer as the system base, you only need to add a new module on the original basis to implement the required functions. The new module has no influence on the original module or has little impact, so you do not need to re-test the original module.
3) There is a strict rule on the litshard replacement principle. The rule is "The subclass must be able to replace the base class, otherwise it should not be designed as its subclass ." That is to say, subclass can only extend the base class, rather than hiding or overwriting the base class,
Advantage: it is easy to implement the interchange of sub-classes under the same parent class, and the client can not notice it.
4) Relying on the switching principle, "design depends on abstraction rather than concrete ". In other words, we should use abstraction to think about the design, rather than first dividing what classes I need, because these are specific.
Advantage: human thinking itself is actually very abstract. When we analyze the problem, we do not consider the details at once, but abstract the whole problem, therefore, abstract design is in line with human thinking. In addition, this principle will be well supported (the open and closed principle) OCP. Abstract-oriented design makes it possible for us to rely too much on implementation, so that expansion becomes possible, this principle is also the cornerstone of another article Design by Contract.
5) the interface isolation principle is "dividing large interfaces into multiple small interfaces" to decouple the system, making it easy to refactor, change, and redeploy.
Advantage: When a software system is extended, the modification pressure will not be transferred to other objects.
6) The Demeter rule or least knowledge principle, which was first formally applied in the Demeter system, is defined as the Demeter rule. It is about "one object should know as few other objects as possible ".
Advantage: eliminate coupling.