6 principles of object-oriented design

OOP basically has six principles, but in fact they are complementary, which means that some principles need to use other principles to realize themselves. The six major principles are as follows:

1) Open-Close Principle (OCP), the open-close principle, is that the design should have good support for extensions, and the modification should be strictly limited. This is the most important and most abstract principle. Basically, what we call Reusable Software is developed based on this principle. Other principles also provide a path to its realization.

2) Liskov Substituition Principle (LSP) , Richter substitution principle , very strict principles, the rule is " subclass must be able to replace the base class (parent class) , otherwise it should not design their subclasses." In other words, the child Classes can only extend the base class, not hide or cover the base class.


3) Dependence Inversion Principle (DIP), relying on the inversion principle, "design should rely on abstraction rather than reification". In other words, when designing, we have to think abstractly, instead of starting to divide which categories I need, because these are concrete. What are the benefits of doing this? Human thinking itself is actually very abstract. When we analyze a problem, we do not consider the details at once, but conceive the whole problem very abstractly, so abstract design is in line with human thinking. In addition, this principle will support OCP very well. The abstract-oriented design allows us to not rely too much on implementation, so that expansion becomes possible. This principle is also the cornerstone of another article "Design by Contract".

4) Interface Segregation Principle (ISP), the principle of interface segregation, "breaking up a large interface into multiple small interfaces", The benefits of this are obvious. I don’t know if it’s necessary to continue the description. In order to save space, I actually just made a small summary of these principles. If you need a deeper understanding, I recommend reading "Java and Patterns." , One of MS MVP: This masterpiece! ^_^

5)  Single responsibility: The function of a class is as single as possible, and the coupling is reduced.

6) Law of Demeter or Least Knowlegde Principle (LoD or LKP), Dimit's rule or the principle of least knowledge , this principle is first formalized in the Demeter system Application, so it is defined as Dimit's law. It is about " an object should learn as little as possible about other objects ". It is another rule on how to loosely couple (Loosely-Coupled).

Well, the above is an introduction to the six principles (or rules). The in-depth study of these principles is exactly how to get the design pattern. After an in-depth understanding, we can begin to look at design patterns. Design patterns are the application of these rules. The well-known design patterns include 23 patterns of Gang of Four (GoF), in addition to Many other well-known models can be studied slowly.

For more technical problem solving prevention, please search Qianfeng PHP, Qianfeng Forum

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/72669915