[Design Mode]-Introduction to Design Mode

1. Introduction to design patterns

1. What is a design pattern

Design pattern, called design pattern in English, is a series of solutions proposed by designers for some recurring problems in object-oriented programming. The term design pattern was first introduced by Erich Gamma from the field of architectural design to computer science in the 1990s.

2. The history of design patterns

Kent Baker and Ward Cunningham used Christopher Alexander's ideas in the field of architectural design to develop design patterns in 1987 and applied this idea to the generation of graphical user interfaces in Smalltalk. A year later, Erich Gamma tried to adapt this idea to software development in his PhD thesis at the University of Zurich. At the same time, James Coplien used the same idea to develop C++ from 1989 to 1991, and then published his book Advanced C++ Idioms in 1991. In this year, Erich Gamma received his Ph.D., and then went to the United States, where he published Design Patterns-Elements of Reusable Object-Oriented Software in collaboration with Richard Helm, Ralph Johnson and John Vlissides, which was included in this book. 23 design patterns. These four authors are also known as Gang of Four (GoF) for their anonymity in the field of software development, and their collaboration in this book led to the breakthrough of software design patterns. Sometimes this anonymous GoF is also used to refer to the book mentioned earlier.

3. The purpose of design patterns

The purpose of design patterns is mainly reflected in three aspects:

  1. Improve learners' thinking skills
  2. Can make the coder's code more standardized, engineered, and improve development efficiency
  3. Improve code reusability, strong readability, high reliability, and good flexibility

4. The four elements of design patterns

  1. Pattern name
  2. problem
  3. solution
  4. effect

Second, the classification of design patterns

The involved modes are mainly divided into three categories, 23 kinds

1. Creational design pattern

Creational Pattern is mainly to abstract the instantiation process of classes, and separate the creation of objects and the use of objects in software modules.
Mainly include:

  1. Singleton mode

Design Pattern-Detailed Singleton Pattern

  1. Factory method pattern

Design Pattern-Detailed Explanation of Factory Pattern

  1. Abstract factory pattern

Design Pattern-Detailed Explanation of Factory Pattern

  1. Builder mode

Design Mode-Detailed Explanation of Builder Mode

  1. Prototype mode

Design Pattern-Detailed Prototype Pattern

2. Structural model

Structural Pattern (Structural Pattern) focuses on how to organize existing classes or objects together to form a more powerful structure. It can be divided into two sub-categories: class structure mode and object structure mode.
Mainly include:

  1. Adapter mode

[Design Mode]-Detailed Explanation of Adapter Mode

  1. Bridge mode

[Design Mode]-Detailed Explanation of Bridge Mode

  1. Decorative pattern

[Design Mode]-Decorator Mode

  1. Combination mode

[Design Mode]-Combination Mode

  1. Appearance mode

[Design Mode]-Appearance Mode

  1. Flyweight model
  2. Agency model

3. Behavioral Model

Behavioral Patterns are used to describe the complex process control of the program at runtime, that is, describe how multiple classes or objects cooperate with each other to complete tasks that a single object cannot complete alone. It involves the responsibilities between algorithms and objects. distribution.
Mainly include:

  1. Template method pattern
  2. Command mode
  3. Iterator mode
  4. Observer mode
  5. Intermediary model
  6. Memo mode
  7. Interpreter mode
  8. State mode
  9. Strategy mode

Design Pattern-Detailed Explanation of Strategy Pattern

  1. Chain of Responsibility Model
  2. Visitor mode

Three, the seven principles of object-oriented

The seven principles of Object Oriented Design (OOD) are a basic lesson that Java programmers need to learn. It can greatly improve the reusability and maintainability of the program, and it is also a great tool for refactoring code. .

1. Principle of opening and closing

The Open-Closed Principle (OCP) means that a program should be open for extension and closed for modification. The so-called opening and closing is actually the restriction principle for the two behaviors of expansion and modification.
To give a simple example, the principle of opening and closing is like our usual work system. The system requires eight hours a day, so this eight hours is not allowed to be modified. However, when we go to get off work and when we leave work, the company can freely decide.

2. Relying on the principle of inversion

The Dependence Inversion Principle (DIP) refers to that when designing code structure, high-level modules should not be based on low-level modules. Both should rely on their abstractions. Abstractions should not rely on details, and details should rely on abstractions. Simply put, it depends on the interface, not the implementation.

3. Single responsibility principle

Single responsibility (simple Responsibility Pinciple, SRP) means that there should not be more than one cause that may lead to class changes. For example, modifying the code corresponding to one responsibility should not cause an exception to the function of another responsibility. The purpose of this is to control the granularity of the class, decouple the objects, and improve cohesion.

4. Interface isolation principle

Interface Segergation Principle (ISP) refers to the use of multiple specialized interfaces to achieve their respective functions instead of a single general interface, and the client should not rely on functional interfaces that he does not need.

5. Dimit Principles

The Law of Demeter (LOD) means that an object should have the least knowledge of other objects, also known as the least-know principle. Try to reduce the degree of coupling between classes. The Dimit principle mainly emphasizes only communicating with friends. For example, the controller only depends on the service, and the service depends on Dao.

6. The Richter Substitution Principle

The Liskov Subsitution Principle (LSP) means that if a code logic is applicable to the parent class, it must be applicable to the child class.

7. Synthetic reuse principle

The principle of composite reuse refers to the use of object combinations instead of integration to achieve the purpose of code reuse.

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/112181401