Seven software architecture design principles

"Spring Core Principles" study notes

tips: core principles of spring design pattern

1. Seven software architecture design principles

1. Open Closed Principle OCP

  • Content: Open to expansion , closed to modification
  • For example: the class diagram is shown below, create a course ICourse interface, different courses create different classes of JavaCourse to implement the interface, if there is a course price reduction, do not modify the getPrice method in the original class, but re-extend the new class JavaDiscountCouse inherit this class and rewrite Price method getPrice. This method violates the Liskov Substitution Principle, so the best way is to rewrite a method getDiscountPrice.
    Insert picture description here

2. Dependency Inversion Principle (DIP)

  • content:
    • The program depends on the abstract interface , not on the concrete implementation . Simply put, it requires abstract programming , not programming , which reduces the coupling between the client and the implementation module. (Public methods and abstractions that do not depend on specific implementations are static classes, and public methods and abstractions that depend on specific implementations are interface classes)
    • Dependency injection with Spring is a meaning
  • Example: The class diagram is shown below. Create a course ICourse interface. Different courses implement the ICourse interface. The Tom class can call different course implementation classes through dependency injection (the three methods of dependency injection: parameter passing, constructor, and setter method)
    Insert picture description here

3. The Single Responsibility Principle (SRP)

  • Content: There is no more than one cause of class change. That is, if a class is responsible for different responsibilities, the different responsibilities are split into different classes.
  • Note: During the use of the single principle, it is not easy to implement, and it cannot guarantee that all classes or methods are a single function, but we must try our best to ensure that it can reduce the cost of later operation and maintenance. At the same time, in the DAO model, why there are four different methods for a class (addition, deletion, and modification), because the basic operations of DAO are these four, so there is no need to split the different methods (that is, the code that should not be changed in the later period is not required Split).

4. Interface Segregation Principle (ISP)

  • Content: Use multiple dedicated interfaces instead of a single summary interface, that is, the class does not need to implement the interfaces it does not need.
  • For example: the class diagram comparison is shown below. The animal interface IAnimal has three interfaces: eat, run, fly and swim. They all have a common method is eat, but the three different animals of land, sea and air have different movement methods, so they cannot be defined The unified interface should be split.
    Insert picture description here

5. Law of Demeter (Lod)

Content: An object should keep a minimum understanding of other objects, also known as the Least Knowledge Principle (LKP), emphasizing that it only communicates with friends , not with strangers . Friends are classes in member variables, input and output parameters of methods .
For example: The class diagram comparison is as follows. The boss Boss needs to check the number of Course Courses released, so I want TeamLeader to count, TeamLeader tells the result to Boss, Boss does not need to know the Course Course information, and its method parameters only need to pass TeamLeader, so and TeamLeader is a friend and not a friend of Course, so avoid introducing the Course class inside the method. The TeamLeader's method parameters include Course, so they are friends with Course, so the Course information should be created and placed in TeamLeader. The first method is to create course information in Boss and pass it on to TeamLeader. This method does not conform to the Dimit principle.
Insert picture description here

6. Liskow Substitution Principle (LSP)

  • content:

    • Inheritance advantages:
      • Improve code reuse, subclasses have methods and properties of parent class;
      • Improve the scalability of the code, the subclass can be similar to the parent class, but different from the parent class, retaining the characteristics of self;
    • Inheritance disadvantages:
      • Intrusiveness: Inheritance is invasive, as long as inheritance must have all the methods and properties of the parent class;
      • Not flexible enough: the subclasses are restricted to a certain extent, reducing the flexibility of the code;
      • High coupling: increased coupling, when the constants, variables or methods of the parent class are modified, the modification of the subclass needs to be considered, so once the parent class has changed, it may cause very bad results, and a lot of code must be refactored .
  • Principle: The cornerstone of inheritance and reuse, only when the derived class can replace the base class, the function is not affected, that is, the base class is not affected by any changes to the subclass, then the base class can be truly reused.

  • Specific methods (to ensure that the subclass replaces the parent class code results are consistent):

    • Subclasses must implement the abstract methods of the parent class , but must not override the non-abstract (implemented) methods of the parent class .
    • You can add your own unique methods in subclasses .
  • When the subclass overrides the superclass method, the preconditions of the method (that is, the formal parameters of the method) are more relaxed than the input parameters of the superclass method .

  • When a child class overrides a parent method or implements an abstract method of the parent class **, the post-conditions of the method (that is, the return value of the method) are stricter than the parent class **.

7. Composite / Aggregate Reuse Principle (CARP)

Content: Try to use object composition (has-a) / aggregation (contains-a) instead of inheritance to reduce coupling between classes.

Published 10 original articles · Likes2 · Visits 1925

Guess you like

Origin blog.csdn.net/yuhao22/article/details/105619370