1. Introduction

Design patterns are the best formal practices programmers can use to solve common problems when designing applications or systems.

Design patterns can speed up the development process by providing tested, proven development paradigms.

Reusing design patterns helps prevent subtle problems that cause major problems, and it also improves code readability for coders and architects familiar with the patterns.

Classification of 2.

There are 23 design Patterns, divided into three broad categories: Creative Patterns, Structural Patterns, and Behavioral Patterns.

  1. Creation pattern

    • The singleton pattern
    • The factory pattern
    • Abstract Factory pattern
    • Builder model
    • The prototype pattern
  2. Structural mode

    • Adapter mode
    • The bridge model
    • Decorator mode
    • Portfolio model
    • The appearance model
    • The flyweight pattern
    • The proxy pattern
  3. Behavioral pattern

    • Template method pattern
    • Command mode
    • Iterator pattern
    • Observer model
    • The mediator pattern
    • Memo mode
    • Interpreter mode
    • The state pattern
    • The strategy pattern
    • Chain of Responsibility model
    • Visitor pattern

3. Six principles

3.1 SRP-Single Responsibility Principle

Definition: A class should have only one cause that causes it to change, otherwise the class should be split.

In layman’s terms, a class has only one responsibility.

The core of the single responsibility principle is to control the granularity of classes, decouple objects, and improve their cohesion. Following the single responsibility principle has the following advantages:

  1. Reduced complexity: The logic of a class that has only one responsibility is certainly simpler than that of a class that has multiple responsibilities.
  2. Improved readability: As complexity decreases, readability increases.
  3. Increased maintainability: Increased readability makes it easier to maintain.
  4. Risk reduction from change: Change is inevitable, and if the single responsibility principle is followed well, when modifying one function, it can significantly reduce the impact on other functions.

3.2 OCP – Open Closed Principle

Definition: a software entity such as modules, classes and interfaces, methods, etc. should be open for extension but closed for modification.

  • Open to extension: Means that existing code can be extended to accommodate new requirements or changes.
  • Closed to change: Means that once the class is designed, it can do its work independently without making any changes to the class.

Abstract constraints and change encapsulation are important means to realize the open and closed principle. For the frequently changing state, it is generally encapsulated as an abstraction.

The functions of the open and closed principle are as follows:

  1. You can improve the ease of testing software by simply testing the extended code.
  2. Increases code reusability: The smaller the granularity, the more likely it is to be reused, and in object-oriented programming, abstract programming improves code reusability.
  3. Can improve the maintainability of software: follow the open and closed principle of the code, high stability, continuity, thus easy to expand and maintain.

3.3 Lsp-Liskov Substitution Principle

Definition: Inheritance must ensure that properties held by the superclass are still true in subclasses.

In layman’s terms, a subclass extends the functionality of the parent class, but does not change the functionality of the parent class.

Its specific definition can be summarized as follows:

  1. A subclass can implement an abstract method of the parent class, but cannot override a nonabstract method of the parent class.
  2. Subclasses can add their own special methods.
  3. When a subclass’s method overrides a parent class’s method, the method’s preconditions (that is, its input parameters) are looser than those of the parent class’s method.
  4. When a subclass’s method implements an abstract method of the parent class, the method’s postcondition (that is, the method’s return value) is stricter or equal to that of the parent class.

The main functions of Richter’s substitution principle are as follows:

  1. Richter’s substitution principle is one of the important ways to realize open – close principle.
  2. It overcomes the disadvantage of poor reusability caused by overwriting the parent class in inheritance.
  3. It is the guarantee of the correctness of the action. That is, class extensions do not introduce new errors into existing systems, reducing the likelihood of code errors.
  4. Enhance the robustness of the program, while changing can be very good compatibility, improve the maintenance of the program, scalability, reduce the risk introduced when the requirements change.

3.4 LKP-least Knowledge Principle

The least knowledge principle is also called Demeter’s rule.

Definition: If two software entities do not communicate directly, then direct calls to each other should not occur and can be forwarded by a third party.

In layman’s terms, don’t talk to strangers.

The core idea is low coupling and high cohesion

The correct use of the least knowledge principle has the following advantages:

  1. The coupling degree between classes is reduced and the relative independence of modules is improved.
  2. As the coupling degree is reduced, the class reusability and system scalability are improved.

However, excessive use of the least knowledge principle will make the system produce a large number of mediation classes, which will increase the complexity of the system and reduce the communication efficiency between modules. Therefore, the application of Demeter’s rule requires repeated trade-offs to ensure high cohesion and low coupling as well as clear system structure.

3.5 ISP-Interface Segregation Principle

Definition: A class’s dependency on another class should be based on the smallest interface.

In layman’s terms, you want to make the interface as detailed as possible and have as few methods in the interface as possible.

When using the interface isolation principle to constrain interfaces, note the following:

  1. Keep interfaces small, but limited: It is true that refinement of interfaces can increase programming flexibility, but if it is too small, it can cause too many interfaces and complicate the design. So do it in moderation.
  2. Customize services for classes that depend on interfaces: expose only the methods that the calling class needs, and hide those it doesn’t. Only by focusing on providing custom services for a module can minimal dependencies be established.
  3. Improve cohesion and reduce external interactions: Enable interfaces to accomplish the most with the fewest methods.

3.6 Dip-dependence Inversion Principle

Definition: a high-level module should not depend on a low-level module; both should depend on its abstraction; Abstractions should not depend on details, details should depend on abstractions.

Core idea: Program to the interface, not to the implementation.

Dependency inversion principle is one of the important ways to realize the open closed principle, which reduces the coupling between the customer and the implementation module.

The dependency inversion principle has the following functions:

  1. The dependency inversion principle can reduce the coupling between classes.
  2. The dependence inversion principle can improve the stability of the system.
  3. The dependency inversion principle can reduce the risks associated with parallel development.
  4. The dependency inversion principle improves code readability and maintainability.

The purpose of the dependency inversion principle is to reduce coupling between classes through interface-oriented programming. So in real programming, this rule can be met in a project by following four main points:

  1. Each class tries to provide an interface, an abstract class, or both.
  2. Variables should be declared as interfaces or abstract classes.
  3. No class should derive from a concrete class.
  4. Follow the Richter substitution principle when using inheritance.

3.7 Summary of six Principles

  • The single responsibility principle tells us to implement a class with a single responsibility;
  • Richter’s substitution tells us not to break the inheritance system;
  • The dependency inversion principle tells us to program for interfaces;
  • The principle of interface isolation tells us to keep interfaces simple and simple when designing them.
  • The least knowledge principle tells us to reduce coupling;
  • The open closed principle is the general principle. It tells us to be open for expansion and closed for modification.