Overview of design patterns, how to classify design patterns according to purpose and scope


      This article mainly introduces design pattern concepts, classification, basic elements, advantages and other design pattern features, laying the foundation for each design pattern blog under the Java Edition Design Pattern column.
Object-oriented design principles: https://blog.csdn.net/qq_34896730/article/details/105352240

1. What is a design pattern

       Design Pattern (Design Pattern) is a set of repeated use, most people know, after the cataloging, code design experience summary, the use of design patterns is to make reusable code easier for others to understand and improve code reliability. A design pattern is a technique for documenting solutions to design problems that are constantly recurring in software systems, and is also a technique for sharing expert design experience.
      Four famous software engineering scholars who called themselves "Gang of Four (GoF, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides)" from 1991 to 1992. They published 23 kinds of Design patterns used more frequently in software development aim to use patterns to unify the gap between analysis, design, and implementation of object-oriented methods.
       GoF defines the design pattern as follows:
       Design pattern is a set of customized solutions for solving a common software design problem in a specific environment, which describes the interaction between objects and classes.

2. Basic elements of design patterns

      Design patterns generally include the pattern name, problem, purpose, solution, effect, example code, and related design patterns. The key elements are the following four.
      1. Pattern Name (Pattern Name) The
      pattern name describes the problem, solution and effect of the pattern in one or two words, in order to better understand the pattern and facilitate communication between developers. Or model results. When learning design patterns, you should first accurately remember the Chinese and English names of the pattern. In the existing class libraries, many class names that use design patterns usually contain the name of the design pattern used. If the class name of a class is XXXAdapter, this class is an adapter class, the adapter pattern is used at design time; if the class name of a class is XXXFactory, then the class is a factory class, it must contain a factory method for returning an instance object of a class .
      2. Problem (Problem) The
      problem describes when the mode should be used. It contains the problems in the design and the reasons for the problems. Some of these problems are some specific design issues, such as how to use objects to encapsulate state or use object representation algorithms, etc., or there may be inflexible classes or object structures in the system, resulting in poor system maintainability. Sometimes, the problem description section of a pattern may contain a series of prerequisites that must be met when using the pattern. For example, when using the bridge mode, there must be two independently changing dimensions of the class in the system, and when using the combined mode, there must be a hierarchy of the whole and part of the system. While describing the problem, it actually determines the environment corresponding to the pattern and the motivation for using the pattern.
      3. Solution
      The solution describes the components of the design pattern, and the interrelationships, responsibilities, and ways of collaboration between these components. Patterns are a universal template. They can be used in various occasions. The solution does not describe a specific and specific design and implementation, but provides an abstract description of the design problem and how to use a combination of elements of general significance ( Class and object combination) to solve this problem, the user must master the class diagram, understand the meaning of each role in the class diagram and the relationship between them, and need to master some core code that implements the pattern in order to facilitate actual development Reasonably apply design patterns.
      4. Effects (Consequences)
      effects describe the effects of mode application and the issues that should be weighed when using modes. The effect mainly includes the analysis of the advantages and disadvantages of the model. We need to know that no solution is 100% perfect, and a reasonable evaluation and selection are required when using the design model. While a model has advantages in some aspects and may have defects in another, it is necessary to consider the effects of the model comprehensively. When evaluating the effect, analyze through the object-oriented design principles, such as judging whether a pattern conforms to the principle of single responsibility or whether it conforms to the principle of opening and closing.
      In addition to the above four basic elements, the complete design pattern description usually includes the alias (other names) of the pattern, the classification of the pattern (the category to which the pattern belongs), and the applicability of the pattern (when the design pattern can be used) , Pattern roles (that is, pattern participants, classes and objects in the pattern, and responsibilities between them), pattern instances (using examples to further deepen the understanding of the pattern), pattern applications (the use of the pattern in existing systems) , Mode expansion (some improvements to this mode, other modes related to it, and other extended knowledge), etc.

3. Classification of design patterns

      There are two general classification methods for design patterns:
      1. Classification according to purpose
      Design patterns can be divided into three types: creative, structural, and behavioral according to the purpose (what the pattern is used for).
      (1) The creation mode is mainly used to create objects. GoF provides 5 creation modes, namely Factory Method, Abstract Factory, Builder, and Prototype. ) And singleton mode (Singleton).
      (2) Structural mode is mainly used to deal with the combination of classes or objects. GoF provides 7 structural modes, namely Adapter mode (Adapter), Bridge mode (Bridge), Combination mode (Composite), Decoration mode (Decorator) , Appearance mode (Facade), fly element mode (Flyweight) and proxy mode (Proxy).
      (3) Behavioral mode is mainly used to describe how classes or objects interact and how to assign responsibilities. GoF provides 11 behavioral modes, namely Chain of Responsibility, Command mode, and Interpreter mode ( Interpreter), Iterator mode, Mediator mode, Memento mode, Memento mode, Observer mode, State mode, Strategy mode, Template Method mode Visitor mode (Visitor).
      2. According to the scope classification
      design mode According to the scope (that is, the mode is mainly used to deal with the relationship between classes or the relationship between objects), it can be divided into two types: class mode and object mode.
      (1)The class model handles the relationship between classes and subclasses. These relationships are established through inheritance and are determined at compile time. It is a static relationship.
      (2) The object model handles the relationships between objects. These relationships change at runtime and are more dynamic.

4. Introduction to the GoF design pattern

      A total of 23 design patterns are described in GoF's classic book "Design Patterns: The Basis of Reusable Object-Oriented Software". These 23 patterns are shown in Table 1, respectively.

Table 1 List of 23 design patterns of GoF
Scope / Purpose Creation mode Structural model Behavioral model
Class pattern Factory method pattern (Class) Adapter mode Interpretive pattern
template method pattern
Object mode Abstract factory pattern
builder pattern
prototype pattern
singleton pattern
(Object) Adapter Mode
Bridge Mode
Combination Mode
Decoration Mode
Appearance Mode
Flyover Mode
Proxy Mode
Chain of responsibility pattern
Command pattern
Iterator pattern
Mediator pattern
Memo pattern
Observer pattern
Status pattern
Strategy pattern
Visitor pattern
Table 2 A brief description of GoF's 23 design patterns
Mode category Mode name Mode description
Creative Patterns Abstract Factory Pattern Provide an interface to create a series of related or interdependent objects without specifying their specific classes
Builder Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations
Factory Method Pattern Define an interface for creating objects, but let subclasses decide which class to instantiate. The factory method pattern delays the instantiation of a class to its subclasses
Prototype pattern Use the prototype instance to specify the type of object to be created, and use this prototype to create a new object
Singleton pattern Ensure that there is only one instance of a class and provide a global access point to access this unique instance
Structural Patterns Adapter pattern Convert the interface of one class into another interface that the customer wants. The adapter pattern allows classes with incompatible interfaces to work together
Bridge Pattern Decouple the abstract part from its implementation part so that both can change independently
Composite Pattern Combining multiple objects to form a tree structure to represent a hierarchical structure with a part-whole relationship. The combination mode allows the client to treat single objects and combined objects uniformly
Decorator Pattern Add some additional responsibilities to the object dynamically. In terms of extended functionality, decorative patterns provide a more flexible alternative than using subclasses
Facade Pattern Provide a unified entrance for a group of interfaces in the subsystem. The appearance mode defines a high-level interface that makes this subsystem easier to use
Flyweight pattern Using sharing technology to effectively support the reuse of a large number of fine-grained objects
Proxy Pattern Provide a proxy or placeholder for an object, and the proxy object to control access to the original object
Behavioral Patterns Chain of Responsibility Pattern Avoid coupling the sender and receiver of a request, connect multiple objects into a chain, and pass the request along the chain until an object can handle it
Command pattern Encapsulate a request as an object, so that different requests can be used to parameterize the client, queue the request or record the request log, and support reversible operations
Interpreter pattern Given a language, define a representation of its grammar, and define an interpreter that uses the representation to interpret sentences in the language
Iterator pattern Provides a method to sequentially access each element in an aggregate object without exposing the internal representation of the object
Mediator Pattern Define an object to encapsulate the interaction of a series of objects. The intermediary model makes each object need not be explicitly referenced to each other, so that it is loosely coupled, and the interaction between them can be independently changed
Memento pattern Capture the internal state of an object without destroying the encapsulation, and save this state outside the object, so that the object can be restored to the original saved state later
Observer pattern Define a one-to-many dependency relationship between objects, so that whenever an object state changes, its related dependent objects are notified and automatically updated
State Pattern Allows an object to change its behavior when its internal state changes. The object seems to modify its class
Strategy Pattern Define a series of algorithms, encapsulate each algorithm, and allow others to replace each other. The strategy model allows the algorithm to change independently of the customers who use it
Template Method Pattern Define the framework of the algorithm in an operation, and delay some steps into subclasses. The template method pattern allows subclasses to redefine certain specific steps of an algorithm without changing the structure of an algorithm
Visitor Pattern Represents an operation that acts on each element in the object structure. The visitor pattern can define new operations on these elements without changing the class of each element
      It should be noted that these 23 design patterns do not exist in isolation, and there are links between many patterns. For example, when operating elements in the object result in the visitor pattern, it is usually necessary to use the iterator pattern and define the termination in the interpreter pattern Symbolic expressions and non-terminal expressions can use combined patterns; in addition, you can design the same system by combining two or more patterns, making full use of the advantages of each pattern while allowing them to work together to complete some More complicated design work.

5. Advantages of design patterns

      The design pattern is a successful design plan that can be achieved by maintainability and reuse from many excellent software systems. Using these plans will avoid doing some repetitive work and can design a high-quality software system. Specifically, the advantages of design patterns are as follows:

      (1) 设计模式融合了众多专家的经验,并以一种标准的形式供广大开发人员所用,它提供了一套通过的设计词汇和一种通用的语言以方便开发人员之间沟通和交流,使得设计方案更加通俗易懂。对于使用不同编程语言的开发和设计人员可以通过设计模式来交流系统设计方案,每一个模式都对应一个标准的解决方案,设计模式可以降低开发人员理解系统的复杂度。
      (2) 设计模式使人们可以更加简单、方便地复用成功的设计和体系结构,将已证实的技术表述成设计模式也会使新系统开发者更容易理解其设计思路。设计模式使得重用成功的设计更加容易,并避免那些导致不可重用的设计方案。
      (3) 设计模式使得设计方案更加灵活,且易于修改。在很多设计模式中广泛使用了开闭原则、依赖倒转原则、迪米特法则等面向对象设计原则,使得系统具有较好的可维护性,真正实现可维护性的复用。在软件开发中合理地使用设计模式可以使系统中的一些组成部分在其他系统中得以重用,而且在此基础上进行二次开发很方便。正因为设计模式具有该优点,所以在JDK、Struts、Spring、Hibernate、JUnit等类库和框架的设计中大量使用了设计模式。
      (4) 设计模式的使用将提高软件系统的开发效率和软件质量,并且在一定程度上节约设计成本。设计模式是一些通过多次实践得以证明的行之有效的解决方案,这些解决方案通常是针对某一类问题最佳的设计方案,因此可以帮助设计人员构造优秀的软件系统,并可以直接重用这些设计经验,节省系统设计成本。
      (5) 设计模式有助于初学者更深入地理解面向对象思想,一方面可以帮助初学者更加方便地阅读和学习现有类库与其他系统中的源代码,另一方面还可以提高软件饿设计水平和代码质量。

发布了59 篇原创文章 · 获赞 20 · 访问量 3622

Guess you like

Origin blog.csdn.net/qq_34896730/article/details/105324092