Introduction to Design Patterns-Introduction to Design Patterns

Design patterns represent best practices and are usually adopted by experienced object-oriented software developers. Design patterns are solutions to general problems faced by software developers in the software development process. These solutions are summed up by many software developers after a long period of trial and error.

A design pattern is a set of code design experience that has been used repeatedly and is known to most people. Using design patterns is to reuse code, make it easier for others to understand, and ensure code reliability. There is no doubt that design patterns are win-win for others and systems. Design patterns make code preparation truly engineering. Design patterns are the cornerstone of software engineering, just like the bricks of a building. The reasonable use of design patterns in the project can perfectly solve many problems. Each pattern has a corresponding principle to correspond to it in reality. Each pattern describes a problem that recurs around us and the problem The core solution is the reason why design patterns can be widely used.

What is GOF (Gang of Four, Gang of Four)?

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

The four authors are collectively called  GOF (Gang of Four) . The design patterns they proposed are mainly based on the following object-oriented design principles.

  • Program the interface rather than the implementation.
  • Use object composition in preference to inheritance.

Use of design patterns

The two main uses of design patterns in software development.

A common platform for developers

The design pattern provides a standard terminology system and is specific to a specific situation. For example, the singleton design pattern means the use of a single object, so that all developers familiar with the singleton design pattern can use a single object, and in this way tell the other party that the program uses the singleton pattern.

Best practice

Design patterns have undergone a long period of development, and they provide the best solutions to common problems faced in the software development process. Learning these patterns helps inexperienced developers to learn software design in a simple and quick way.

Types of design patterns

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

Serial number Mode & Description include
1 Creational patterns
These design patterns provide a way to hide the creation logic while creating an object, instead of instantiating the object directly with the new operator. This makes the program more flexible in determining which objects need to be created for a given instance.
  • Factory Pattern
  • Abstract Factory Pattern
  • Singleton Pattern (Singleton Pattern)
  • Builder 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 combine interfaces and define the ways in which combined objects obtain new functions.
  • Adapter Pattern
  • Bridge Pattern
  • Filter mode (Filter, Criteria Pattern)
  • Composite Pattern (Composite Pattern)
  • Decorator Pattern (Decorator Pattern)
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern
3 Behavioral patterns
These design patterns pay special attention to communication between objects.
  • Chain of Responsibility Pattern (Chain of Responsibility Pattern)
  • Command Pattern
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern (Mediator Pattern)
  • Memento Pattern (Memento Pattern)
  • Observer Pattern
  • State Pattern
  • Null Object Pattern
  • Strategy Pattern (Strategy Pattern)
  • Template Pattern
  • Visitor Pattern (Visitor Pattern)
4 J2EE patterns
These design patterns pay special attention to the presentation layer. These modes are certified by Sun Java Center.
  • MVC Pattern (MVC Pattern)
  • Business Delegate Pattern
  • Composite entity pattern (Composite Entity Pattern)
  • Data Access Object Pattern (Data Access Object Pattern)
  • Front Controller Pattern
  • Intercepting Filter Pattern
  • Service Locator Pattern
  • Transfer Object Pattern (Transfer Object Pattern)

Here is a picture to describe the relationship between design patterns as a whole:

设计模式之间的关系

Six principles of design patterns

1. Open Close Principle

开闭原则的意思是:对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。简言之,是为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。

2、里氏代换原则(Liskov Substitution Principle)

里氏代换原则是面向对象设计的基本原则之一。 里氏代换原则中说,任何基类可以出现的地方,子类一定可以出现。LSP 是继承复用的基石,只有当派生类可以替换掉基类,且软件单位的功能不受到影响时,基类才能真正被复用,而派生类也能够在基类的基础上增加新的行为。里氏代换原则是对开闭原则的补充。实现开闭原则的关键步骤就是抽象化,而基类与子类的继承关系就是抽象化的具体实现,所以里氏代换原则是对实现抽象化的具体步骤的规范。

3、依赖倒转原则(Dependence Inversion Principle)

这个原则是开闭原则的基础,具体内容:针对接口编程,依赖于抽象而不依赖于具体。

4、接口隔离原则(Interface Segregation Principle)

这个原则的意思是:使用多个隔离的接口,比使用单个接口要好。它还有另外一个意思是:降低类之间的耦合度。由此可见,其实设计模式就是从大型软件架构出发、便于升级和维护的软件设计思想,它强调降低依赖,降低耦合。

5、迪米特法则,又称最少知道原则(Demeter Principle)

最少知道原则是指:一个实体应当尽量少地与其他实体之间发生相互作用,使得系统功能模块相对独立。

6、合成复用原则(Composite Reuse Principle)

合成复用原则是指:尽量使用合成/聚合的方式,而不是使用继承。

https://www.runoob.com/design-pattern/design-pattern-intro.html

Guess you like

Origin blog.csdn.net/wxb880114/article/details/110522844