Gof23 Design Pattern Diagram

Source: Internet
Author: User
Gof23 design patterns User Manual
Object-Oriented Design Patterns and principles
Design Mode Summary
  Creation type Structural Behavior Type
Mode name Singleton
Abstract Factory
Builder
Factory method
Prototype
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Template Method
Command
Interpreter
Mediator
Iterator
Observer
Chain of responsibility
Memento
State
Strategy
Visitor
Mode Diagram

Name Singleton returns to the top
Structure
Intention Ensure that a class has only one instance and provides a global access point for the instance.
Applicability
  • When a class can only have one instance and the customer can access it from a well-known access point.
  • When this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code.
  • Name Image factory mode (Abstract Factory) returns the top
    Structure
    Intention Provides an interface for this interface to create a series of "related or interdependent objects" without specifying their specific classes.
    Applicability
  • A system must be independent of the creation, combination, and representation of its products.
  • A system must be configured by one of multiple product generations.
  • When you want to emphasize the design of a series of related product objects for joint use.
  • When you provide a product class library, instead of displaying their interfaces.
  • Name Builder mode (builder) returns to the top
    Structure
    Intention Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
    Applicability
  • When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
  • When the constructor must allow different representations of the constructed object.
  • Name Factory method returns the top
    Structure
    Intention Defines an interface used to create objects, so that the subclass determines which class to instantiate. Factory method delays the instantiation of a class to the subclass.
    Applicability
  • When a class does not know the class of the object it must create.
  • When a class wants its subclass to specify the object it creates.
  • When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.
  • Name Prototype returns to the top
    Structure
    Intention Use the prototype instance to specify the object type, and then copy the prototype to create a new object.
    Applicability
  • When the class to be instantiated is specified at the runtime, for example, through dynamic loading;
  • To avoid creating a factory class level parallel to the product class level;
  • When an instance of a class can only have one of several combinations of different States. Creating prototype and cloning them may be more convenient than manually instantiating this type with the appropriate state each time.
  • Name Adapter mode returns to the top
    Structure
    Intention Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
    Applicability
  • You want to use an existing class, and its interface does not meet your needs.
  • You want to create a reusable class that can work together with other unrelated classes or unforeseen classes (that is, classes whose interfaces may not be compatible.
  • (Only applicable to object a d a p t e r) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.
  • Name Bridge returns to the top
    Structure
    Intention Separate the abstract part from the implementation part so that they can all change independently.
    Applicability
  • You do not want to have a fixed binding relationship between the abstraction and its implementation. For example, this may be because the implementation part of the program should be selected or switched during the runtime.
  • Class abstraction and its implementation should be expanded by generating sub-classes. In this case, the B r I d G E mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
  • The modification of an abstract implementation part should not affect the customer, that is, the customer's Code does not need to be re-compiled.
  • (C ++) You want to completely hide the abstract implementation part of the customer. In C ++, the class representation is visible in the class interface.
  • There are many classes to generate. Such a hierarchical structure indicates that an object must be divided into two parts. R u m B A u g h refers to this type of hierarchy as "nested generalization" (nested generalizations ).
  • You want to share the implementation among multiple objects (the reference count may be used), but the customer is not required to know this. A simple example is c o p l I e n s t r I N G class [c o p 9 2], multiple objects in this class can share the same string representation (s t r I n g r e p ).
  • Name Composite returns to the top
    Structure
    Intention Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the use of a single object and a composite object consistent.
    Applicability
  • The part of the object-the overall hierarchy.
  • You want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner.
  • Name Decorator returns to the top
    Structure
    Intention Dynamically add some additional responsibilities (functions) to an object ). The decorator mode is more flexible than the subclass generation function.
    Applicability
  • Add roles to a single object dynamically and transparently without affecting other objects.
  • Handle unrecoverable responsibilities.
  • When the subclass generation method cannot be used for expansion. One case is that there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Another scenario is that the class definition is hidden, or the class definition cannot be used to generate a subclass.
  • Name Facade returns to the top
    Structure
    Intention Provides a consistent interface for a group of sub-system interfaces. The facade mode defines a high-level interface, which makes the sub-system easier to use.
    Applicability
  • When you want to provide a simple interface for a complex subsystem. Subsystems tend to become more and more complex as they evolve. In most modes, More and smaller classes are generated. This makes subsystems more reusable and easier to customize, but it also brings some difficulties for users who do not need to customize subsystems. F a c a d e can provide a simple default view, which is sufficient for most users, those who need more customization can go beyond the f a c a d e layer.
  • There is a large dependency between the implementation part of the client program and the abstract class. Introducing f a c a d e to separate the sub-system from the customer and other sub-systems can improve the independence and portability of the sub-system.
  • When you need to build a layered sub-system, use the f a c a d e mode to define the entry points for each layer of the sub-system. If subsystems are mutually dependent, you can allow them to communicate only through f a c a d e, thus simplifying the dependency between them.
  • Name Flyweight returns to the top
    Structure
    Intention Use the sharing technology to effectively support a large number of fine-grained objects.
    Applicability
  • An application uses a large number of objects.
  • Because a large number of objects are used, the storage overhead is very high.
  • Most States of an object can be changed to external states.
  • If you delete the external state of an object, you can replace multiple groups of objects with a relatively small number of shared objects.
  • Applications do not rely on object identifiers. Because the f l y w e I g h t object can be shared, the logo test returns the true value for an object that is conceptually distinct.
  • Name Proxy mode returns the top
    Structure
    Intention Provides a proxy for other objects to control access to this object.
    Applicability
  • The p r o x y mode is used to replace simple pointers with common and complex object pointers. The following are some common cases where the p r o x y mode can be used: 1) remote proxy provides a local representation of an object in different address spaces. Nextstep [add94] uses the N x p r o x y class to achieve this goal. Coplien [cop92] calls this proxy "Ambassador" (a m B A s a d o r ). 2) virtual proxy creates objects with high overhead as needed. I m a described in motivation section
    G e p r o x y is an example of such a proxy. 3) protection proxy controls access to the original object. Protection proxy is used when the object should have different access permissions. For example, in the C h o I c e s operating system [c I r m 9 3], k e m e l p r o x I e s provides access protection for operating system objects. 4) Smart reference replaces simple pointers and performs some additional operations when accessing objects. Its typical uses include:
  • The reference count pointing to the actual object. When this object is not referenced, it can be automatically released (also known as s m a r tp o I n t E R S [E d e 9 2]).
  • When a persistent object is referenced for the first time, it is loaded into the memory.
  • Before accessing an actual object, check whether it is locked to ensure that other objects cannot change it.
  • Name Template Method returns the top
    Structure
    Intention Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
    Applicability
  • Implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.
  • Common behaviors in each subclass should be extracted and concentrated into a common parent class to avoid code duplication. This is a good example [o j 9 3] of "redecomposition to generalization" described by o p d y k e and J o h n s o n. First, identify the differences in the existing code and separate the differences into new operations. Finally, replace these different codes with a template method that calls these new operations.
  • Control subclass extension. The template method only calls the "h o k" operation at a specific point (see effect), so that the extension can only be performed at these points.
  • Name Command returns the top
    Structure
    Intention Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support auditable operations.
    Applicability
  • Like the m e n u I t E M object discussed above, extract the action to be executed to parameterize an object. You can use the callback (c a l B A C K) function in the process language to express this parameterization mechanism. A callback function is a function that is registered at a certain place and will be called at a later time. The c o m a n d mode is an object-oriented alternative to the callback mechanism.
  • Specify, arrange, and execute requests at different times. A c o m a n d object can have a lifetime unrelated to the initial request. If the recipient of a request can be expressed in a way unrelated to the address space, the command object responsible for the request can be sent to another different process and implemented there.
  • Cancel the operation. The c o m a n d e x c u t e operation can store the status before performing the operation. This status is used to eliminate the impact of the operation when the operation is canceled. The c o m a n d interface must add a u n e x e c u t e operation, which cancels the effect of the last e x e c u t e call. The executed command is stored in a history list. You can traverse this list backward and forward and call u n e x e c u t e and redo ".
  • Logs can be modified so that the modifications can be redone when the system crashes. Add the loading and storage operations to the c o m a n d interface to maintain a consistent change log. Recovery from a crash involves re-reading the recorded commands from the disk and re-executing them with the e x e c u t e operation.
  • Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (t r a n s a c t I o n. A transaction encapsulates a group of changes to the data. The c o m a n d mode provides a method for modeling transactions. C o m a n d has a common interface that allows you to call all transactions in the same way. At the same time, it is easy to add new transactions to expand the system.
  • Name Interpreter mode (Interpreter) returns to the top
    Structure
    Intention Given a language, it defines a syntax expression and an interpreter that uses this representation to interpret sentences in the language.
    Applicability
  • When a language needs to be interpreted and executed, and you can represent sentences in the language as an abstract syntax tree, you can use the interpreter mode. This mode works best in the following situations:
  • This grammar is simple for complex grammar, and the class layers of grammar become huge and cannot be managed. At this time, the syntax analysis program generator is a better tool. They can interpret expressions without building an abstract syntax tree, which saves space and time.
  • Efficiency is not a key issue. The most efficient interpreter is usually not implemented by directly interpreting the syntax analysis tree. Instead, they are first converted into another form. For example, regular expressions are usually converted to state machines. However, even in this case, the converter can still be implemented in interpreter mode, which is still useful.
  • Name Mediator mode returns to the top
    Structure
    Intention An intermediary object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.
    Applicability
  • A group of objects communicate in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand.
  • An object references many other objects and communicates directly with these objects, making it difficult to reuse the object.
  • We want to customize a behavior distributed in multiple classes without generating too many child classes.
  • Name Iterator mode (iterator) returns to the top
    Structure
    Intention Provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
    Applicability
  • Access the content of an aggregate object without exposing its internal representation.
  • Supports multiple traversal of aggregate objects.
  • Provides a unified interface (supporting multi-state iteration) for Traversing different aggregation structures ).
  • Name Observer returns to the top
    Structure
    Intention Define a one-to-many dependency between objects so that when the state of an object changes, all objects dependent on it will be notified and updated automatically.
    Applicability
  • When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.
  • When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.
  • When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.
  • Name Chain of responsibility returns to the top
    Structure
    Intention So that multiple objects have the opportunity to process the request, so as to avoid coupling between the request sender and the receiver. Connect these objects into a chain and pass requests along the chain until an object processes it.
    Applicability
  • Multiple objects can process one request, and the object that processes the request is automatically determined at runtime. You want to submit a request to one of multiple objects without specifying the receiver explicitly.
  • The object set that can process a request should be dynamically specified.
  • Name Memento returns to the top
    Structure
    Intention Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
    Applicability
  • You must save the (partial) state of an object at a certain time point so that it can be restored to the previous state later.
  • If an interface is used to directly obtain these States for other objects, the implementation details of the object will be exposed and the object encapsulation will be broken.
  • Name Status mode (state) returns to the top
    Structure
    Intention Allows an object to change its behavior when its internal state changes. So that the object seems to have modified its behavior.
    Applicability
  • The behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
  • An operation contains a large number of conditional statements with multiple branches, and these branches depend on the state of the object. This state is usually represented by one or more enumerated constants. Generally, multiple operations contain the same condition structure. In the s t a t e mode, each condition branch is placed into an independent class. This allows you to take the object state as an object based on the object's own situation. This object can be independent of other objects.
  • Name Strategy returns to the top
    Structure
    Intention Define a series of algorithms, encapsulate them one by one, and make them replace each other. This mode allows the algorithm to change independently of its customers.
    Applicability
  • Many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions.
  • Different variants of an algorithm are required. For example, you may define algorithms that reflect different space/time trade-offs. When these variants are implemented as class layers of an algorithm [h o 8 7], the policy mode can be used.
  • Algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex algorithm-related data structures.
  • A class defines multiple behaviors, and these behaviors appear in the form of multiple conditional statements in the operations of this class. Move related condition branches into their respective s t r a t e g y classes to replace these condition statements.
  • Name Visitor mode (visitor) returns to the top
    Structure
    Intention Indicates an operation that acts on each element in an object structure. It can define new operations that act on these elements without changing the classes of each element.
    Applicability
  • An object structure contains many class objects with different interfaces. You want to perform operations on these objects dependent on their specific classes.
  • You need to perform many different and unrelated operations on the objects in an object structure, and you want to avoid causing these operations to "pollute" the classes of these objects. Vi S I t o r allows you to set relevant operations in a class. When the object structure is shared by many applications, use the vi S I t o r mode to allow each application to only include the operations needed.
  • Classes that define the object structure are rarely changed, but new operations are often needed in this structure. Changing the object structure class requires redefining the interfaces for all visitors, which may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes.
  •  
    This comment is forwarded to Weibo and forwarded to Weibo.

    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.