"Design Principles" of Design Patterns Written for Myself

Design Principles

Transfer from: https://www.cnblogs.com/pony1223/p/7594803.html

1. Single responsibility

Definition
A class is only responsible for one responsibility

advantage

  • Reduce the complexity of the class, the logic is simple
  • Improve the readability of the class and improve the maintainability of the system
  • Reduce the risk of change, with fewer impact points

2. The Richter Substitution Principle

Definition
Wherever a base class appears, a subclass must appear. Subclasses can extend the functions of the parent class, but cannot change the original functions of the parent class

Advantages of inheritance

  • The subclass has all the properties and methods of the parent class, reducing the workload of creating the class
  • Improve code reusability
  • Improve code scalability, subclasses not only have the functions of the parent class, but can also have their own functions

Disadvantages of inheritance

  • Inheritance is invasive. As long as you inherit, you must have all the properties and methods of the parent class.
  • Reduce the flexibility of the code. Because when inheriting, the parent class will have a constraint on the child class.
  • Enhanced coupling. When you need to modify the code of the parent class, you must consider the impact on the subclass. Sometimes if you modify a little code, you may need to refactor the interrupt program.

rule

  • Subclasses can implement the abstract methods of the parent class, but cannot override non-abstract methods
  • Subclasses can add their own methods
  • When a subclass overrides or overloads a method of the parent class, the method entry is more relaxed than the parent class (parent class: ArrayList, subclass: List)
  • When the child class implements the method of the parent class, the method return value is more specific than the parent class

3. Relying on the principle of inversion

The definition
depends on abstraction, not on concrete implementation. High-level modules should not depend on low-level modules, and both should rely on their abstractions; abstractions should not depend on details, and details should depend on abstractions. Interface-oriented programming.

advantage

  • Interface-oriented programming, reduce the coupling between classes, improve system stability, and reduce the risk of program modification

rule

  • Low-level modules should have abstract classes or interfaces as much as possible, or both (may be used by people)
  • The declaration type of the variable should be an abstract class or interface as much as possible
  • Follow the Richter substitution principle when using inheritance

4. Interface isolation principle

Definition The
client should not rely on interfaces it does not need; the dependence of one class on another should be based on the smallest interface.将臃肿的接口拆分成几个接口,类按需建立依赖关系

advantage

  • Reduce the degree of coupling between classes and provide program flexibility

rule

  • The interface is as small as possible, but there must be a quota. Too few programs are inflexible, and too many designs complicate.
  • Customize services for classes that rely on the interface, and only expose the methods it needs to the calling class, and hide the methods it doesn’t need
  • Improve cohesion and reduce external interaction. Make the interface use the least methods to accomplish the most things

5. Dimit's principle (least know the principle)

Definition
An object should have a minimum understanding of the dependent objects. Regardless of the complexity of the dependent class, try to encapsulate the logic inside the class as much as possible, provide public methods to the outside, and not leak any information.

advantage

  • Reduce coupling

rule

  • Minimize the coupling relationship between classes

6. Principle of opening and closing

Definition is
open for extension, closed for modification

7. Summary

Build a framework with abstraction, and expand details with implementation

  • 单一职责原则 Single responsibility for implementation class
  • 里氏替换原则 Don't break the inheritance system
  • 依赖倒置原则 Interface-oriented programming
  • 接口隔离原则 Be concise and simple when designing interfaces
  • 迪米特原则 Reduce coupling.
  • 开闭原则 General outline, open for extension, closed for modification

Guess you like

Origin blog.csdn.net/Dkangel/article/details/105579783