23 Design patterns and their application scenarios

Source: Internet
Author: User

Design patterns are divided into three types: created, structured, and behavioral.
Among the created types are:
First, Singleton, Singleton mode: Ensure that a class has only one instance, and provide a global access point to access it; scenario: A stateless class uses Singleton mode to conserve memory resources.

Abstract Factory, Abstraction Factory: Provides an interface that creates a series of related or interdependent objects without specifying their specific classes. Application Scenario: A series of interdependent objects have different implementations. Provides a "encapsulation mechanism" to avoid tight coupling between the client program and this "multi-series concrete object creation work".

Factory method, Factory methods: Define an interface for creating objects, let subclasses decide which class to instantiate, and Factory method to defer instantiation of a class to subclasses. Application scenario: The subclass of a class often faces drastic changes due to changes in demand, but he has a relatively stable interface. Using a encapsulation mechanism to "isolate changes to this volatile object", the factory method defines an interface for creating objects, allowing subclasses to determine which concrete class of objects to create and defer instantiation of the object.

Builder, Build mode: Separates the construction of a complex object from his presentation, allowing the same build process to create different representations. Application scenario: The specific implementation classes or algorithms for each component of a class are often subject to change, but the algorithms that combine them are relatively stable. Provides a encapsulation mechanism to isolate stable combinatorial algorithms from the various components that are variable.

V. Prototype, prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes. Application scenario: Creating an object with new requires very tedious data preparation or permissions


Behavioral types are:
Vi. Iterator, Iterator mode: Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. Application Scenario: Iteration.


VII. Observer, Observer pattern: Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified of Automatic Updates. Scenario: Changes to an instance affect multiple other objects.


Template method: Define the skeleton of an algorithm in an operation, and defer some steps into subclasses, Templatemethod so that subclasses can redefine some specific steps of the algorithm without altering the structure of an algorithm. Application scenario: An operational step is stable, while the specific details of the change are deferred to the subclass


commands, Command mode: Encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue requests and log requests, and support revocable operations. Scenario: Completely decouple the command from the performer.


State mode: Allows an object to change his behavior when its internal state changes.           The object seems to have changed his class. Application scenario: When an object's internal state changes, his behavior changes dramatically.


XI. strategy, Strategy mode: Define a series of algorithms, encapsulate them one by one, and allow them to replace each other, this mode allows the algorithm to be independent of the customers who use them. Application Scenarios:

12, China of Responsibility, responsibility chain mode: Enables multiple objects to have the opportunity to process the request, thus avoiding the coupling between the requested communicated and the receiver

13, Mediator, Mediator mode: Encapsulates the object interaction of some columns with a mediation object.

14. Visitor, Visitor mode: represents an operation that acts on elements of an object structure, allowing you to define new actions that act on this element without changing the individual element classes.

XV, interpreter, interpreter mode: Given a language, define a representation of his grammar and define an interpreter that uses that representation to interpret sentences in the language.

16. Memento, Memo mode: captures the internal state of an object without destroying the object, and saves the state outside the object.

Structural types are:
17, Composite, combination mode: The object is combined into a tree structure to represent a partial overall relationship, and Composite makes the user consistent with the use of individual objects and composite objects.

18, facade, appearance mode: To provide a consistent interface for a set of interfaces in the subsystem, Fa?ade provides a high-level interface, which makes the subsystem easier to use.

19, Proxy, Agent mode: Provide a proxy for other objects to control access to this object

20, Adapter, adapter mode: To convert a class of interfaces into another interface that the customer wants, the Adapter mode makes it possible for those classes to work together because the interface is incompatible and cannot work together.

21, Decrator, Decoration mode: Dynamically add some additional responsibilities to an object, the decorator mode is more flexible than generating subclasses in terms of the added functionality.

22. Bridge mode: Separates the abstract part from its realization, so that they can change independently.
23, Flyweight, enjoy meta mode

23 Design patterns and their application scenarios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.