The five Principles of object-oriented
single principle of responsibility SRP (Single Responsibility Principle)
Refers to the function of a class to be single, not all-encompassing. Like a person, the distribution of work can not be too much, otherwise busy all the way, but the efficiency is not high up.
Open Closure Principle OCP (Open-Close Principle)
A module should be open in terms of extensibility and should be closed in terms of change. For example: A network module, the original service-only function, and now to join the client function,
Then, without modifying the service-side function code, you can increase the client function implementation code, which requires that at the beginning of the design, the server should be separated from the client, the public part of the abstraction.
The principle of the Richter replacement (The Liskov Substitution Principle LSP)
Subclasses should be able to replace the parent class and appear anywhere the parent class can appear. For example: The company engaged in annual party, all employees can participate in the lottery, so whether the old staff or new employees,
Whether it is the headquarters staff or the outside staff, should be able to participate in the lottery, or the company will not be harmonious.
Dependency Inversion principle (The Dependency inversion Principle DIP) depending on the abstraction, the upper layer relies on the lower layer. Suppose b is a lower module, but b needs to use the function of a ,
this time, B should not be used directly A the specific class in: and should be made by B defines an abstract interface and is defined by the A to implement this abstract interface, B Use this abstract interface only: This will achieve
The purpose of dependency inversion, B also lifted the A dependence, which in turn is A relies on B defines the abstract interface. It is difficult to avoid relying on the lower module through the upper module, if B is also directly dependent on The implementation of a, then it may cause cyclic dependence. A common problem is that you need to include the CPP file directly into the B module When compiling the A module , while compiling B is also included directly in the A of the CPP file.
Dimitri Law
Dimitri (lawofDemeter) is also known as the least knowledge principle (Least knowledge Principle Shorthand LKP ), which means that an object should have as little knowledge of other objects as possible , don't talk to strangers.
The English shorthand is : LoD. The Dimitri Law can be simply said: Talk your immediate friends . for OOD , it is also explained in the following ways: a software entity should interact with as few interactions as possible with other entities. Each software unit has only minimal knowledge of the other units, and is limited to the software units that are closely related to the unit.
The purpose of the Dimitri law is to reduce the coupling between classes. Since each class minimizes its reliance on other classes, it is easy to make the functional modules of the system independent of each other, and there is no (or little) dependency between them.
The Dimitri law does not want the class directly to establish direct contact. If there really is a need to establish a connection, it is also hoped that it will be conveyed through its friend class. Therefore, one of the possible consequences of applying the Dimitri law is that there are a large number of intermediary classes in the system that exist solely to convey the mutual invocation relationship between classes-which in some way increases the complexity of the system.
It is interesting to study the design pattern of the façade mode (facade) and the mediation mode (Mediator), Are examples of the application of the Dimitri Law.
Object oriented (Five principles)