Seven principles of design pattern (Java)

Time:2020-1-11

1、 Three basic characteristics of OOP

OOP

As a new method, the essence of object-oriented programming is the abstract thinking process and object-oriented method embodied by the establishment of model. The model is used to reflect the characteristics of things in the real world. It is impossible for any model to reflect all the specific characteristics of the objective things. It can only abstract the characteristics of things and the law of change, and describe the characteristics of objects more generally, more intensively and more profoundly in the scope it involves. The abstraction achieved by establishing models is the deepening of people’s understanding of objects.

encapsulation

Encapsulation is to encapsulate the objective things into abstract classes, and classes can only let trusted classes operate their own properties and methods, and hide the untrusted information.

inherit

Inheritance is the ability to use all the functions of an existing class and extend them without having to rewrite the original class.

polymorphic

Polymorphism means that the same method of a class instance has different forms in different situations. Specifically, different implementation classes have different implementation methods for the public interface, but these operations can be called in the same way (public interface).

2、 Seven principles of OOD

There are seven principles of object-oriented design (OOD), which complement each other

Open close principle

Open close principle (OCP). Open means to open an extension, that is, to support convenient extension; closed means to close a modification, that is, to strictly limit the modification of existing content. Open close principle is the most abstract and important ood principle. Simple factory mode, factory method mode and abstract factory mode all mentioned how to follow the open close principle through good design.

Richter’s principle of substitution

Liskov Substitution Principle (LSP). The principle states that “a subclass must be able to replace its parent, otherwise it should not be designed as a subclass.”. In other words, where the parent class appears, it should be able to be replaced by its child class. Therefore, the subclass can only extend the base class, not hide or cover the base class.

Principle of Dependence Inversion

Dependency Inversion Principle (DIP). It says that “design and implementation depend on abstraction rather than concrete”. On the one hand, abstraction is more in line with people’s thinking habits; on the other hand, according to the Richter’s replacement principle, it is easy to replace the original abstraction with the expanded concrete, which can well support the open close principle.

Interface isolation principle

Interface segmentation principle (ISP), the principle of interface isolation, “breaking up a large interface into several small independent interfaces”. Because Java classes support multiple interfaces, it is easy to make classes have the characteristics of multiple interfaces, and each class can selectively only implement the target interface.

Principle of single responsibility

Single responsibility principle (SRP). It says that there should be no more than one reason for class changes, which is an embodiment of high cohesion and low coupling.

Dimitar’s law / the least known principle

Law of Demeter or least knowledge principle (LOD or LKP). It’s about “one object understands as few other objects as possible” to achieve loose coupling. If a class has too many responsibilities, any change of responsibilities may cause problems of other responsibilities due to the coupling of multiple responsibilities, which seriously affects the maintainability and reusability of the code.

Principle of composite / Aggregate Reuse

Composite / Aggregate Reuse Principle (carp / CRP). If some functions of the new object have been implemented in other created objects, the functions provided by other objects should be used as much as possible to make it a part of the new object instead of re creating. New objects can reuse existing functions by delegating them. In short, try to use composition / aggregation rather than inheritance.