Six principles of design patterns

Open-Closed Principle


The Open- Closed Principle means that development is open for extension and closed for modification. In layman's terms, it means that a software entity (such as: class, function, module, etc.) should be able to be extended by developers, but cannot modify the content already specified in the entity.

When the software needs to change, try to achieve the change by extending the behavior of the software entity, rather than modifying the existing code to achieve the change.

The open-closed principle is the most basic design principle, and the other five design principles are specific forms of the open-closed principle.

The Liskov Substitution Principle


The Liskov Substitution Principle states that in a subclass, members of the base class should be able to access correctly. That is, subclasses cannot override or modify members of the base class.

When using inheritance, the Liskov substitution principle should be followed. When class B inherits class A, in addition to adding new methods to implement new functions, it should try not to override the methods of parent class A, and try not to overload the methods of parent class A.

Dependency Inversion Principle


1. High-level modules should not depend on low-level modules, they should all depend on abstractions.

2. Abstraction should not depend on concrete implementation, concrete implementation should depend on abstraction.

The Dependency Inversion Principle is that programs should depend on abstract interfaces, not on concrete implementations. Simply put, it is required to program the abstraction, not the implementation, which reduces the coupling between the client and the implementation module.

Interface Segregation Principle

A class's dependency on another class should be established on the smallest interface. Otherwise, classes that inherit from this interface must implement methods they do not need.

Synthesis/aggregation principle Use some existing objects

in a new object to make it a part of the new object; the new object achieves the purpose of reusing the existing functions by delegating to these objects. Its design principle is: try to use composition/aggregation, try not to use inheritance.

principle of minimum knowledge

An object should have minimal knowledge about other objects. The fewer the connections between the two classes, the smaller the impact on the other class when one class changes, so that the coupling between classes can be reduced as much as possible.

Guess you like

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