basic design principles

 Six basic design principles:

1. Single Responsibility Principle

2. Liskov substitution principle

3. Dependency Inversion Principle

4. Interface isolation principle

5. The principle of Demeter

6. The principle of opening and closing

 

 

Single Responsibility Principle ( Srp )

Definition: There should be one and only one reason for a class change. That is, the single responsibility principle requires that an interface or class has only one reason for change, that is, an interface or class has only one responsibility, and he is responsible for one thing.

Responsibility is a concept that is difficult to define. We must grasp a degree, that is, to divide responsibilities reasonably, without too strong coupling and too many classes. (Principle is dead, man is alive)

Benefits :

1. The complexity of the class is reduced, and the responsibilities to be implemented are clearly defined;

2. Improved readability;

3. Improve maintainability;

4. The risk caused by the change is reduced, and the change is indispensable. If the single responsibility of the interface is done well, an interface modification only affects the corresponding implementation class and has no effect on other interfaces, which will affect the scalability of the system, Maintainability is very helpful.

Liskov Substitution Principle

Definition: As long as the parent class appears, the subclass can appear, and replacing it with the subclass will not produce any errors or exceptions, and the user may not need to know the parent class or the subclass at all. However, the reverse is not the case. Where there are subclasses, the parent class may not be able to adapt.

 

In an object-oriented language, inheritance is essential;

The advantages of inheritance are:

1. Code sharing, reducing the workload of the created class, each subclass has the methods and properties of the parent class;

2. Improve code reusability;

3. The subclass can be similar to the parent class, but different from the parent class;

4. Improve the scalability of the code;

5. Improve the openness of products or projects

shortcoming:

1. Inheritance is invasive. As long as you inherit, you must have all the properties and methods of the parent class;

2. Reduce the flexibility of the code. The subclass must have the properties and methods of the parent class, which makes the free world of the subclass more restrictive;

3. Enhanced coupling. When the constants, variables and methods of the parent class are modified, the modification of the subclass needs to be considered, and in the absence of a standard environment, this modification may cause a large section of code to need to be refactored ;

Dependency Inversion Principle ( must be used for large projects )

definition:

       1. High-level modules should not depend on low-level modules, both should depend on their abstractions;

       2. Abstraction should not depend on details;

       3. Details should depend on abstraction ;

Definition of high-level modules and low-level modules: the implementation of each logic is composed of atomic logic, the inseparable atomic logic is the low-level module, and the reassembly of the atomic logic is the high-level template;

Abstract: Refers to an interface or an abstract class, both of which cannot be directly instantiated;

Details: Details are the implementation classes, and the classes generated by implementing interfaces or inheriting abstract classes are details, and their characteristics are that they can be instantiated, that is, an object that can be generated by adding a keyword new .

 

The performance of the Dependency Inversion Principle:

1. Dependency between modules occurs through abstraction, and there is no direct dependency relationship between implementation classes, and its dependency relationship is generated through interfaces or abstract classes;

2. The abstract or implementation class does not depend on the implementation class;

3. The implementation class depends on the interface or abstract class;

 

That is, interface-oriented programming;

 

The essence of Dependency Inversion is to make each class or module independent from each other through abstraction (interface or abstract class), without affecting each other, to achieve true loose coupling;

Reflection in the project:

1. Each class should have an interface or an implementation class as much as possible, or both abstract classes or interfaces;

2. The surface type of the variable should be an interface or an abstract class as much as possible;

3. No class should be derived from a concrete class;

4. Try not to override the method of the base class;

5. That is to use the principle of rational substantiation;

Interface Segregation Principle

Interface Type:

1. Instance interface: declare a class in Java , and then use the new keyword to generate an instance, which is a description of a type of thing, which is an interface; for example, you define a Person class, and then use Person zhangsan = new Person(); generates an instance, where the Person class is the interface of zhangsan ;

 

2. Class interface: the interface defined by the interface keyword often used in Java ;

 

Isolation Definition:

1. The client should not depend on the interface it does not need; depending on the interface it needs, the client provides whatever interface the client needs, and removes the unnecessary interface, which requires refinement of the interface to ensure its purity;

2. The dependencies between classes should be established on the smallest interface; it requires the smallest interface, but also requires the interface to be refined and the interface is pure;

That is to establish a single interface, don't build a bloated interface. Generally speaking, the interface should be as detailed as possible, and the methods in the interface should be as few as possible; a single responsibility requires that the class and interface have a single responsibility, and the focus is on the responsibility, which is business logic. division. The interface isolation principle requires as few interface methods as possible, pay attention to distinction.

 

Best Practices:

       The interface isolation principle is the definition of the interface, but also the definition of the class, the interface and the class should use the atomic interface and class as much as possible;

1. An interface only serves one sub-module or business logic;

2. Compress public methods in the interface through business logic

3. If the interface has been polluted, try to modify it as much as possible. If the risk of change is high, use the adapter mode for conversion processing;

4. Understand the environment, refuse to blindly follow and do not blindly imitate the code on the Internet, but to deeply understand the logic and design the best interface.

Law of Demeter

Definition : An object should have the least knowledge about other objects. Generally speaking, a class should know the least about the class that it needs to be coupled or called. It doesn't matter to me how complicated the interior of the coupled or called class is;

1. Only communicate with direct friends:

 

 

The core concept of Demeter's Law is decoupling and weak coupling between classes. Only after weak coupling can the reuse rate of classes be improved.

 

open-closed principle    

definition:

A software entity such as class, module, and function should be open for extension and closed for modification; (a software entity should implement changes by extension, not by modifying existing code)

 

Software entities include:

1. Modules in a project or software product divided according to certain logical rules

2. Abstraction and classes

3. Method

 

 

Guess you like

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