1. Principle of single responsibility

Introduction: For classes, a class should have only one responsibility.

For example, Type A is responsible for different responsibilities: Responsibility 1, responsibility 2; When responsibility 1 is changed, it may lead to abnormality of responsibility 2, so it is necessary to split type A into two classes, which are responsible for responsibility 1 and responsibility 2 respectively, so that they do not affect each other.

Objective:

  1. Reduce the complexity of classes so that each class has only one responsibility.

  2. Improve readability and maintainability of classes.

  3. Reduce the risk of change.

  4. The principle of uniformity at the method level can only be maintained if there are enough methods in a class.

2. Open and close principle

Introduction: programming the most basic, the most important design principles.

The core idea is to extend development (relative provider) and close to modify (relative user).

Build the framework with abstractions and extend the details with implementations.

3. Interface isolation principle

Introduction: A class’s dependence on another class should be based on a minimal interface. For example, interface A has five methods, and interfaces of class B and D need 123 methods to implement interface A, and 234 methods to implement interface D. This violates the interface isolation principle. The five methods should be split into different interfaces or abstractions, and BD needs functions to inherit or implement the corresponding interfaces or abstractions.

Note: A class depends on another class through an interface. The dependency interface is expected to be minimal and the methods that are not needed can be isolated through the interface.

4. Rely on the inversion principle

Introduction: High-level modules should not depend on low-level modules; both should depend on abstractions (abstract classes or interfaces).

Abstractions should not depend on details, details should depend on abstractions.

The central idea is interface oriented programming.

Dependency pass is divided into interface pass, constructor pass, setter method pass.

Details:

  1. Low-level modules should have abstractions (abstract classes or interfaces) as far as possible, so that the program stability is good.

  2. Variable declaration type is as far as possible abstract class or interface, so that variable reference and the actual object, there is a buffer layer, conducive to program expansion, optimization.

5. Richter’s substitution principle

Reason: Inheritance brings both advantages and disadvantages to programming. The use of inheritance can make the program intrusive, reduce the portability of the program, increase the object coupling, one class is inherited by another class, when the class changes, it affects all the subclasses, all the functions involved in the subclass will also be affected.

Introduction: Try not to override methods of a parent class in a subclass. Richter’s substitution principle tells us that inheritance makes two classes more coupled and can be reduced through aggregation, composition, and dependency.

Demeter’s Rule

Definition: also known as the least know principle; It can also be defined as communicating only with direct friends.

Introduction: An object should know the least about its objects. The less a class knows about its dependent classes, the better. For dependent classes, try to encapsulate logic inside the class and provide public methods.

Direct friends: As long as there is a coupling relationship between objects, we say there is a friend relationship between objects. There are many ways of coupling, such as dependency, association, composition, aggregation, etc. The classes in member variables, method parameters and method return values are direct friends. Unfamiliar classes are best left out of local variables inside the class, which violates Demeter’s law.

The core idea of Demeter’s law is to reduce coupling between classes. It reduces the coupling between classes, not the absence of dependencies.

7. Principle of composite reuse

Try to use composition/aggregation rather than inheritance.

Core ideas of design principles:

  1. Look for things that might change in your application, isolate them, don’t mix them with things that don’t change.

  2. Program for interfaces, not implementations.

  3. Strive for loose coupling design between interacting objects.

Conclusion:

The open and closed principle is the general principle. It tells us to be open to expansion and closed to modification. Richter’s substitution tells us not to break the inheritance system; The dependency inversion principle tells us to program for interfaces; The single responsibility principle tells us to implement a class with a single responsibility; The principle of interface isolation tells us to keep interfaces simple and simple when designing them. Demeter’s rule tells us to reduce coupling; The principle of composite reuse tells us to use composite or aggregate relation reuse in preference to inheritance relation reuse. In practice, you can use design patterns based on the business, but it’s important not to get bogged down by these rules.