Introduction to design patterns


Design patterns represent best practices and are typically used by experienced object-oriented software developers. Design patterns are solutions to common problems faced by software developers during the software development process. These solutions are the result of trial and error by numerous software developers over a long period of time.

A design pattern is a set of code design experiences that are repeatedly used, known to most people, classified and cataloged. The purpose of using design patterns is to reuse code, make the code easier to understand by others, and ensure code reliability. There is no doubt that design patterns are win-win for ourselves, others, and the system. Design patterns make code compilation truly engineering. Design patterns are the cornerstone of software engineering, just like the bricks and stones of a building. Reasonable use of design patterns in projects can perfectly solve many problems. Each pattern has corresponding principles in reality to correspond to it. Each pattern describes a problem that keeps recurring around us, and the solution to the problem. Core solution, which is why design patterns can be widely used.

What is GOF (Gang of Four)?

In 1994, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides co-authored a book called Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - Reusable Object-Oriented Software Elements) book, which first mentioned the concept of design patterns in software development.

The four authors are collectively known as GOF (Gang of Four, spelled out in full Gang of Four). The design patterns they proposed are mainly based on the following object-oriented design principles.

  • Programm to the interface rather than to the implementation.

  • Prefer object composition to inheritance.

The use of design patterns

There are two main uses of design patterns in software development.

A common platform for developers

Design patterns provide a standard terminology system that is specific to specific scenarios. For example, the singleton design pattern means using a single object so that all developers who are familiar with the singleton design pattern can use the single object and can tell each other that the program uses the singleton pattern.

Best Practices

Design patterns have been developed over a long period of time and they provide the best solutions to common problems faced during software development. Learning these patterns helps less experienced developers learn software design in a quick and easy way.

Types of design patterns

According to the reference book of design patternsDesign Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - Reusable Object-Oriented Software Elements ) mentioned in, there are a total of 23 design patterns. These patterns can be divided into three major categories: Creational Patterns, Structural Patterns, and Behavioral Patterns. Of course, we'll also discuss another category of design patterns: J2EE design patterns.

##3## 4J2EE Patterns

The following uses a picture to describe the overall relationship between design patterns:

the-relationship-between-design-patterns.jpg

The six principles of design patterns

1. Open Close Principle

The Open Close Principle means: Open for expansion, closed for modification. When the program needs to be expanded, the original code cannot be modified to achieve a hot-swappable effect. In short, it is to make the program scalable and easy to maintain and upgrade. To achieve this effect, we need to use interfaces and abstract classes, which we will mention in the specific design later.

2. Liskov Substitution Principle

The Liskov Substitution Principle is one of the basic principles of object-oriented design. The Liskov Substitution Principle says that wherever a base class can appear, a subclass can definitely appear. LSP is the cornerstone of inheritance reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can be truly reused, and the derived class can also add new ones on the basis of the base class. Behavior. Liskov substitution principle is a supplement to the opening and closing principle. The key step to realize the opening and closing principle is abstraction, and the inheritance relationship between base classes and subclasses is the specific implementation of abstraction, so the Liskov substitution principle is a specification for the specific steps to achieve abstraction.

3. Dependence Inversion Principle

This principle is the basis of the opening and closing principle. The specific content: for interface programming, rely on abstraction rather than Depends on the specifics.

4. Interface Segregation Principle

This principle means: using multiple isolated interfaces is better than using a single interface. It also has another meaning: reducing the coupling between classes. It can be seen that design patterns are actually software design ideas that start from large-scale software architecture and are easy to upgrade and maintain. It emphasizes reducing dependencies and reducing coupling.

5. Demeter's Law, also known as the Demeter Principle

The Least Known Principle means: an entity should interact with other entities as little as possible Interactions occur between them, making the system functional modules relatively independent.

6. Composite Reuse Principle

The Composite Reuse Principle means: Try to use synthesis/aggregation instead of inheritance.

Serial NumberMode & Descriptionincludes
1Creational Patterns
These design patterns provide a way to hide the creation logic while creating an object, rather than instantiating the object directly using new operators. This allows the program to be more flexible in determining which objects need to be created for a given instance.
  • Factory Pattern

  • Abstract Factory Pattern

  • Singleton Pattern

  • Builder Pattern

  • Prototype Pattern

2 Structural Patterns
These design patterns focus on the combination of classes and objects. The concept of inheritance is used to compose interfaces and define the way in which composed objects acquire new functionality.
  • Adapter Pattern

  • Bridge Pattern

  • Filter Pattern (Filter, Criteria Pattern)

  • Composite Pattern (Composite Pattern)

  • Decorator Pattern (Decorator Pattern)

  • Facade Pattern

  • Flyweight Pattern

  • Proxy Pattern Pattern)

Behavioral PatternThese design patterns pay special attention to the relationship between objects Communication.
  • Chain of Responsibility Pattern

  • Command Pattern

  • Interpreter Pattern

  • Iterator Pattern

  • Mediator Pattern

  • Memento Pattern

  • Observer Pattern

  • State Pattern (State Pattern)

  • Null Object Pattern

  • Strategy Pattern

  • Template Pattern

  • Visitor Pattern

These design patterns focus specifically on the presentation layer. These modes are certified by Sun Java Center.
    MVC Pattern
  • Business Delegate Pattern
  • Composite Entity Pattern
  • Data Access Object Pattern
  • Front-end Controller Pattern( Front Controller Pattern)
  • Intercepting Filter Pattern
  • Service Locator Pattern
  • Transfer Object Pattern