01. Classification and principles of design patterns

   Design patterns are the classics of software programming, and they are also the quintessence of predecessors' continuous experience summarization. It is like the sunflower collection in java software programming. It is best practice for specific problems and scenarios. Learning design patterns will help us gain a deeper understanding of the idea and essence of programming.
    There are 23 design patterns summed up by predecessors, which can be divided into three categories according to their application: ① creation type, ② structure type, and ③ behavior type.

Six principles of design patterns:
①Open-Closed Principle, ②Liskov Substitution Principle, ③Dependency Inversion Principle, ④Interface Isolation Principle, ⑤Least Knowing Principle, ⑥Composite Reuse Principle

1. Open Close Principle

The open-closed principle means: open for extension, closed for modification . When the program needs to be expanded, the original code cannot be modified to achieve a hot-plug effect. In short, it is to make the program expandable, easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific design later.

2. Liskov Substitution Principle

The Liskov Substitution Principle is one of the basic principles of object-oriented design. The Liskov Substitution Principle states that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance and reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can be truly reused, and the derived class can also add new ones on the basis of the base class. behavior. The Liskov Substitution Principle is a complement to the Open-Closed Principle. The key step to realize the open-closed principle is abstraction, and the inheritance relationship between the base class and the subclass is the concrete realization of abstraction, so the Liskov substitution principle is the specification of the concrete steps to realize abstraction.

3. Dependence Inversion Principle

This principle is the basis of the open-closed principle, concrete content: programming against interfaces, relying on abstraction rather than concreteness.

4. Interface Segregation Principle

What this principle means is that it is better to use multiple isolated interfaces than to use a single interface. It has another meaning: reduce the coupling between classes. It can be seen that, in fact, the design pattern is a software design idea that starts from a large-scale software architecture and is easy to upgrade and maintain. It emphasizes reducing dependencies and coupling.

5. The Law of Demeter, also known as the Principle of Least Knowing (Demeter Principle)

The principle of least-knowing means that an entity should interact with other entities as little as possible, so that the functional modules of the system are relatively independent.

6. Composite Reuse Principle

The principle of composition reuse means: try to use composition/aggregation instead of inheritance.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324921949&siteId=291194637