This is the 27th day of my participation in Gwen Challenge

Name of design Principle Set the righteous Use frequency
Single Responsibility Principle (SRP) A class is responsible for only one function area U u u u do
Open-closed Principle (OCP) Software entities should be open for extension and closed for modification U u u u u
Liskov Substitution Principle (LSP) All references to objects of the base class can transparently use objects of its subclasses U u u u u
Dependence Inversion Principle (DIP) Abstraction should not depend on details, details should depend on abstractions U u u u u
Interface Segregation Principle (ISP) Use multiple specialized interfaces instead of a single master interface U u being fostered fostered
Composite Reuse Principle (CRP) Use object composition rather than inheritance for reuse purposes U u u u do
Law of Demeter (LoD) A software entity should interact with as few other entities as possible U u u do do

The open closed principle

  • The open close principle is the first cornerstone of object-oriented reusable design, and it is the most important object-oriented design principle
  • Abstraction is the key to the open close principle

Single responsibility principle

  • The single responsibility principle is the simplest object-oriented design principle used to control the granularity of a class
  • The single responsibility principle is the guideline to achieve high cohesion and low coupling. It is the simplest but most difficult principle to use, requiring designers to discover different responsibilities of classes and separate them, while discovering multiple responsibilities of classes requires designers to have strong analysis and design ability and relevant practical experience

Omega substitution principle

  • Replacing a base object with a subclass object in software does not generate any errors or exceptions. The reverse is not true. If a software entity uses a subclass object, it may not be able to use a base object
  • In the program, try to use the base class type to define the object, and at run time to determine its subclass type, with the subclass object to replace the parent class object

Rely on the inversion principle

  • One of the main implementation mechanisms of object-oriented design, it is the concrete implementation of system abstraction
  • Dependency injection means that when an object is dependent on another object, the dependent object is injected through abstraction. There are three common injection methods, which are: construct injection, Setter injection and interface injection

Interface separation principle

  • Each interface should assume a relatively independent role, not doing what should not be done, do what should be done
  • The interface only provides the behavior that the client needs, and the behavior that the client does not need is hidden, and the client should be provided with the smallest possible individual interface, rather than a large overall interface
  • When using the interface isolation principle, you need to control the interface granularity. If the interface is too small, the system will be flooded with interfaces, which is not conducive to maintenance. The interface should not be too large. Too large interfaces violate the interface isolation principle, resulting in poor flexibility and inconvenient use

Principle of composite reuse

  • Use composite/aggregate relationships (associations) rather than inheritance when reuse
  • Due to the combination or paradigmatic relations existing objects can be members (or objects) into the new object, make it become a part of the new object, so the function of the new object can be called the existing object, such doing can make members object’s internal implementation details for the new object is not visible, so this kind of reuse, also known as “black box” reuse, relative to the inheritance relationship, Its coupling degree is relatively low, and the change of member object has little effect on the new object

Demeter principle

  • Demeter’s law can reduce the coupling degree of the system and keep loose coupling relationship between classes
  • Interaction between objects should be minimized. If two objects do not have to communicate directly with each other, they should not have any direct interaction, and if one of them needs to call a method of the other object, the call can be forwarded by a third party

Summary: These seven design principles are the principles that software design patterns must follow as far as possible, and each principle requires different emphases. Among them, the “open and closed principle” is the general outline, which tells us to “open to expansion, closed to modification”; Richter’s Substitution principle tells us not to destroy the inheritance system; [Dependency inversion principle] tells us to [interface oriented programming]; The principle of single responsibility tells us that to achieve [class], we need [single responsibility]; The interface isolation principle tells us to design interfaces with simplicity and simplicity. [Demeter’s law] tells us to [reduce coupling degree]; The principle of composite reuse tells us to give priority to composite or aggregate relation reuse and less to inheritance relation reuse.