[Design Pattern]-The design principles and purposes of design patterns

Preface

Reference video link

Seven design principles commonly used in design patterns

single responsibility principle

For classes, a class should only be responsible for one responsibility

If a class is responsible for two responsibilities, when one of the responsibilities changes and the class needs to be modified, it may cause errors in the execution of the other responsibility, so the class should be decomposed

It is not necessary to decompose classes blindly to achieve the principle of single responsibility. Blindly decomposing classes will lead to too many classes and excessive cost. It is also necessary to consider implementing the principle of single responsibility at the method level in a class. Of course, the number of methods is also Not too much.

Interface isolation principle

The client should not rely on interfaces it does not need. The dependence of one class on another class should be based on the smallest interface

Scenes

There are five methods in an interface interface, class C and class D implement the interface interface and must implement these five methods, and then class A depends on class C through the interface, but class A only uses methods 1 and 4 in the interface, 5; Class B depends on class D through the interface, but class B only uses methods 1, 2, and 3 in the middle of the interface. This creates a situation where classes C and D must implement methods they don't need.

According to the principle, we should deal with it like this: split the interface into several independent interfaces, and class A and class B respectively establish dependencies with the interfaces they need. Specifically, the interface is divided into three interfaces, one interface contains 1 method, one interface contains 4, 5 methods, and the remaining interface contains 2, 3 methods, and then let C implement the first and second interfaces, and D go Implement the first three interfaces

dependency inversion principle

Design concept : Compared with the variability of details, abstract things are much more stable; the architecture based on abstraction is much more stable than the architecture based on details. In Java, abstraction can be said to be interfaces and abstract classes. The implementation class is the detail. We should rely on abstractions rather than concrete

There are three delivery methods for dependencies : interface delivery, constructor method delivery, and setter method delivery.

Note :

  1. Low-level modules should try to have abstract classes or interfaces or both.
  2. The declared type of the variable should be an abstract class or interface as much as possible, so that there is a buffer layer between the variable reference and the actual object, which is conducive to program expansion and optimization.
  3. Follow the Liskov substitution principle when inheriting

Liskov Substitution Principle

About Inheritance in OO

  1. The meaning of inheritance is that the methods that have been implemented in the parent class are actually setting specifications and contracts. Although it is not mandatory for all subclasses to follow these contracts, if subclasses arbitrarily modify these implemented methods , it will cause damage to the entire inheritance system.
  2. The disadvantage of inheritance is that the use of inheritance will bring intrusion to the program, reduce the portability of the program, and increase the coupling between objects. If a class is inherited by other classes, all subclasses must be considered when this class needs to be modified. And after the parent class is modified, all functions involving the subclass may malfunction. Therefore, core classes like String are not allowed to be inherited.

The same argument for the Liskov substitution principle is: all places that refer to the base class must be able to use objects of its subclass transparently, or in other words, when the parent class reference is changed from pointing to the parent class object to pointing to the subclass object, the behavior of the program No changes will follow. This requires that when using inheritance, try not to override the methods of the parent class in the subclass.

Scenes

Suppose now there is a class A, a class B, B inherits A and rewrites the method in A, then we should deal with it like this: create
a base class on top of A and B, let A and B both inherit This base class, so that A and B are decoupled. If B wants to call the method in A later, it can be realized by aggregation, dependency, combination, etc. For example, a member variable of class A is created in B, and then B The class object calls the method in A by calling this A variable.

Open-Closed Principle ocp (Open-Closed Principle)

The open-closed principle is the most basic and important design principle in programming. A software entity, such as a class, module, or function, should be open for extension and closed for modification. When the software needs to change, try to achieve the change by extending the behavior of the software entity rather than by modifying the existing code.

The framework should be abstract and expand its details when we implement it

Law of Demeter

Also known as the principle of least knowledge , that is, the less a class knows about the classes it depends on, the better. Public method, does not provide any information

Demeter's Law has a simpler definition: only communicate with direct friends. As long as there is a coupling relationship between two objects, we call them friends. The coupling methods include dependency, association, combination, aggregation, etc. Among them, we call the classes that appear in member variables, method parameters, and method return values ​​​​as direct friends. , and the classes that appear in local variables are not direct friends, which means that it is best not for unfamiliar classes to appear in the class as local variables.

The core of Dimit's Law is to reduce coupling, which does not mean there is no dependence at all.

Synthetic Reuse Principle

Try to use combination or aggregation instead of inheritance

Combination , such as A is composed of B, which means that A contains the global object (local variable) of B, and B is also creatednew when A is created (use instantiation when declaring a local variable of type B in A ; except In addition, if A in the program defines the cascade deletion of B, then A also combines B). The whole and the parts cannot be separated

Aggregation , such as A aggregates B, means that there may be a reference to B in A, but B is not created when A is created, it may be created through a setmethod (declaring a local variable of type B but only declaring without assigning a value). The whole and the parts can be separated. Combination is more coupled than aggregation. Whether to determine the life cycle of aggregated/combined variables is a major difference between the two.

The purpose of design patterns

  1. code reusability
  2. readability
  3. Scalability (maintainability)
  4. reliability
  5. High cohesion and low coupling

Guess you like

Origin blog.csdn.net/Pacifica_/article/details/120295848