23 kinds of GoF Design Patterns Overview

23 kinds of GoF Design Patterns Overview

In front of our 23 design patterns of GoF categorized, where the first of the functions of each design pattern of a brief introduction, in order to have a general idea. Later chapters further detail.

Create a schema

Focus on how to create a schema object is created, they will create and use objects are separated, the user object without concern for how to create objects, only know how to use on the line in order to reduce the degree of coupling. Like a car without paying attention to the use of the car is how people made out as long as they know how to use on the line. The following five modes that fall into this category.

  • Singleton (the Singleton) mode: the control can only generate a class instance of a global access for external self. For example: Windows window manager or task manager is only one instance.
  • Prototype (the Prototype) mode: create a high cost (eg: loading large files, time-consuming initialization, the CPU resource consumption wait) as a prototype of an object by its copy or clone, creating a new instance of other similar.
  • Abstract Factory (Abstract Factory) mode: each is created by the concrete factory inherited from the abstract factory classes have many different products of the same contact. For example, different training school, you can create courses and course materials used.
  • Builder (Builder) mode: for a complex object, it requires many steps and build member, such objects will be disassembled into a plurality of relatively simple components or steps, and then they were built as needed, until the complex object. For example: fast food packages, he constructed separately, drinks, French fries consists of Hamburg, which requires producers to build or create a variety of items, respectively, and then return to a full course meal staff.
  • Factory method (Factory Method) mode: determined by the particular plant derived from the abstract factory class generating any specific product. For example, belong to the furniture factory sofa factory, tables, chairs and bed factory production plant are sofas, chairs and bed furniture.

Structural model

This focus on how to model objects and classes according to a certain manner together to form a new, larger, more loosely coupled structure effect, the composition of this embodiment for inheritance class structure, and a structure for combining objects or polymeric . The degree of coupling between objects than the inheritance relationship between the various objects or polymeric compositions having a weak relationship, so that the object structure than the class object has a lower degree of coupling. 7 kinds of design patterns belong structural model is as follows:

  • Bridge (Bridge) mode: This mode contains two classes, an abstract class and implementation class, in an abstract class (note that this is not an abstract class inherits inside said abstract class) by reference to the reality of class (a reference to the abstract like a bridge connection and implementation classes up) to achieve separation and the abstract so that they can be varied independently. This mode uses a combination relation, rather than to achieve inheritance, and abstraction implemented to reduce the dimensions of the two variable degree of coupling.
  • Decoration (Decorator) mode: dynamically add new objects to the state and behavior, to make access to other extra features. This extension way than to take inheritance to generate implementation subclass is more flexible. This model is a key point to be decorated decoration in succession at the same time contains its instance.
  • Proxy (Proxy) mode: client to access a complex object indirectly through a proxy object. Agent herein as complex object mini-jack, played a complex object controls access to the functions, including the control limit, strengthen, verify or modify some features of the object. The most common proxy mode as a local agent of some kind remote access services.
  • Appearance (Facade) mode: an interface based on a number of complex subsystems, providing a higher level of consistency on which external interfaces, these subsystems have been easy to use and access. For example, a key shopping, which orders, shipping addresses, and so on receivables in this step is complete, the appearance of the model used here.
  • Flyweight (the Flyweight) mode: Depending on whether as the environment changes, the public interface to distinguish between variable and immutable objects two states, i.e. the internal state and external state, the internal state of the same portion as the need to implement a shared, can external change in the form of parameters passed to the method, in order to support reuse a lot of fine-grained objects.
  • A combination of (Composite) mode: The combination of object hierarchy, such as tree structure, so that a user can set or objects treated in the same manner. That is consistent with the user access to individual objects and combinations of objects. Such as Windows File Explorer, you can file, a folder the same operation.
  • Adapter (Adapter) mode: In order to resolve the incompatibility interface converts the interface of a class into another interface clients can use, so that they can work together. For example, you want to use third-party components, but your current system with its different interfaces, and you do not want to change it, you need to use an adapter to solve this problem.

Behavioral models

Process control how to handle multiple interactions between classes or objects and collaborative problem or run-time type of model for object roles distribution and control algorithms are part of his treatment areas. The following 11 kinds of design patterns fall into this category:

  • Duty chain (Chain of Responsibility) mode: a plurality of requesting processors have a common interface objects together into a chain process, the first object requests from the chain transfer onward, until the request is processed. Reimbursement approval escalation is a typical example.
  • Command (Command) mode: request encapsulated into command objects to decouple the relationship between the request issued by the executive. The restaurant meal is a specific example, a menu command, when the recipient chef, waiter or customer is the command issuer.
  • Observers (Observer) mode: there is a dependency between the object and one or more objects, when the state of the object changes, the object of dependencies will be automatically notified and responds accordingly. This mode is also known as publish - subscribe pattern. Such as changes in customer relationships with customers concerned about the selling price of the product.
  • Mediator (Mediator) mode: establishing an intermediary to process the mutual interaction between a plurality of objects. In this way each object becomes interactive with intermediaries, rather than mutual interactions between objects. This can reduce the degree of coupling between objects that do not have direct correlation between the original reference object. As a net-like structure into a star configuration, each other to reduce the dependence. For example micro-channel group is a typical example of the mediator, all group members can interact with each other through a channel between the micro base, rather than for each other between.
  • State (State) mode: for stateful objects, the behavior of its change of state extracts encapsulated state, such a state where an object when its internal state changes, the behavior changed, thereby changing the behavior of an object . This pattern is common in games or workflow systems. For example, do not have the approval of funding, we are being reviewed, is being processed, has been completed and other states.
  • Iterator (the Iterator) mode: the polymerization object collection element, without the use of the understanding of its internal structure, to provide an access interface which the collection order. Common applications such as picture browsing, which can first map, Previous, Next, the end of the map to view the pictures, is a typical iterative mode applications.
  • Visitors (Visitor) mode: without changing the original type definition, all elements of the existing structure to define and implement the new operation. In this way, each element can provide a variety of new operations, that is, each element has multiple visitors Object Access, and perform a new operation. Common goods such as the supermarket to buy, variety, and some need to weigh, and some need to be packaged, the cashier scan code they need for valuation, these different operations can be achieved through the visitor pattern.
  • Memorandum (Memento) mode: If you want to change the recording state of an object at a time for recovery, the object can be encapsulated in the memo previous state (objects) mode, and the object to the memo and the memo storage management, for subsequent use. The most common withdrawal (Undo) is achieved in this way.
  • Strategy (Strategy) mode: In many cases, for a given problem, there are many algorithms or strategies to resolve. In order to avoid these algorithms all conditional branch statements in a host class, we can treat each algorithm is pulled out is formed with a class, or all of these algorithms are implemented inherit a same abstract class or interface, and a context class with select and hold each of their algorithm or strategy, and as a client of the entrance. For example, various sorting algorithms can be used in this way to organize.
  • Interpreter (Interpreter): is used or interpreted language represented in a method as defined instructions or statements. Similar to the compiler to interpret the code.
  • The method of template (Template Method) mode: a plurality of operation steps in the algorithm by the fixed structures, of which some of the steps are implemented changeable, put these particular steps in changing subclass to redefine. The process of loading data from the database: connection, access data, process the data, close the connection, the process for getting any data from the database are the same fixed, but data acquisition and data processing for different business, his implementation details is not the same.

 

Guess you like

Origin www.cnblogs.com/markkang/p/11832513.html