Design mode + optimistic lock/pessimistic lock

Source: Internet
Author: User

First, overview:    A class relationship represents: Description:     two. Create Type 1.Factory Method


Intentions:defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory Method defers the instantiation of a class to its subclasses.  Applicability:
    • When a class does not know the class of the object it must create.
    • When a class wants to specify the object it creates by its subclasses.
    • When a class delegates the responsibility of creating an object to one of several helper subclasses, and you want to set which helper subclass is the agent this information is localized.
2.Abstract Factory    


Intentions:provides an interface to create a series of related or interdependent objects without specifying their specific classes. Applicability:
    • A system is independent of the creation, composition, and presentation of its products.
    • When a system is to be configured by one of multiple product families.
    • When you want to emphasize the design of a series of related product objects for joint use.
    • When you provide a Product class library and just want to display their interfaces instead of implementing them.
  3. Builder  Intentions:separating the construction of a complex object from its representation allows the same build process to create different representations.  Applicability:
    • When creating complex objects, the algorithm should be independent of the parts of the object and how they are assembled.
    • When the construction process must allow the constructed object to have different representations.
4. Prototype   Intentions:Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.Applicability:
    • When the class to instantiate is specified at run time, for example, by dynamic loading;
    • To avoid creating a factory class hierarchy that is parallel to the product class hierarchy;
    • When an instance of a class can have only one of several different state combinations. Building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state.
5.Singleton  Intentions:Ensure that a class has only one instance and provides a global access point to access it.Applicability:
    • When a class can have only one instance and the customer can access it from a well-known access point.
    • When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.
three. Structural Type 6.Adapter  Intentions:Transforms the interface of one class into another interface that the customer wants. A d a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.Applicability:
    • You want to use a class that already exists, and its interface doesn't fit your needs.
    • You want to create a reusable class that can work with other unrelated classes or classes that are not predictable (that is, those that might not necessarily be compatible with the interface).
    • (Applies to object a D a P T e r) you want to use some subclasses that already exist, but it is not possible to subclass each to match their interfaces. The object adapter can be adapted to its parent class interface.
7.Bridge   Intentions:Separate the abstract part from its implementation, so that they can all change independently.Applicability:
    • You don't want to have a fixed binding relationship between the abstraction and its implementation section. For example, this may be because the implementation part of the program run time should be selectable or switched.
    • The abstraction of a class and its implementation should be augmented by methods that generate subclasses. At this point, the B r i d g E mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
    • Modifications to an abstract implementation section should not have an impact on the customer, i.e. the customer's code does not have to be recompiled.
    • (C + +) you want to completely hide the abstract implementation part from the customer. In C + +, the representation of a class is visible in the class interface.
    • There are many classes to build. Such a class hierarchy illustrates that you must break up an object into two parts. R u m b a u g H call this class hierarchy "nested nested" (generalizations).
    • You want to share the implementation across multiple objects (you might use reference counting), but you're asking customers not to know that. A simple example is C o p L i e n S t r i n g class [C O P 9 2], in which multiple objects can share the same string representation (s t r i n g r e P).
8.Composite  Intentions:Combines objects into a tree structure to represent a "partial-whole" hierarchy. C o m P o s i t e makes the user consistent with the use of a single object and a composite object.

Applicability:
    • You want to represent the part of the object-the overall hierarchy.
    • You want users to ignore the difference between a combined object and a single object, and the user will use all the objects in the composite structure uniformly.
9.Decorator   Intentions:Add some extra responsibilities to an object dynamically. For added functionality, the D e c o r a t o mode is more flexible than generating subclasses.Applicability:
    • Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.
    • Handle the duties that can be undone.
    • When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
10.Facade   Intentions:Providing a consistent interface for a set of interfaces in a subsystem, the F a c a D e pattern defines a high-level interface that makes this subsystem easier to use.Applicability:
    • When you want to provide a simple interface for a complex subsystem. Subsystems tend to become more complex as they evolve. Most patterns will produce more and smaller classes when used. This makes the subsystem more reusable and easier to customize the subsystem, but it also brings some usability difficulties to users who do not need to customize the subsystem. f a c a D e can provide a simple default view that is sufficient for most users, and those who need more customization can cross the f a c a D e layer.
    • There is a large dependency between the client program and the implementation part of the abstract class. The introduction of f a c a D e separates the subsystem from the customer and other subsystems, which can improve the independence and portability of the subsystem.
    • When you need to build a hierarchical subsystem, use the f a C a D e pattern to define the entry points for each layer in the subsystem. If subsystems are interdependent, you can make them communicate only through f a c a D e, simplifying the dependencies between them.
11.Flyweight     Intentions:Use shared technology to effectively support a large number of fine-grained objects.Applicability:
    • An application uses a large number of objects.
    • Because of the use of a large number of objects, resulting in a large storage overhead.
    • Most of the state of an object can become an external state.
    • If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects.
    • The application does not depend on the object identity. Since f l y w e i g H-t objects can be shared, the identity test returns true for objects that are conceptually distinct.
12.Proxy  Intentions:Provides a proxy for other objects to control access to this object.Applicability:
    • Use P r o x y mode when you need to replace a simple pointer with a more general and complex object pointer. Here are some common scenarios where you can use P r o x y mode:
      1) Remote Proxy provides local representation of an object in different address spaces. NEXTSTEP[ADD94] uses n x P r o x Y class to accomplish this. COPLIEN[COP92] called the agent "Ambassador" (a M b a s S A d o r).
      2) virtual agent (virtual proxy) creates expensive objects as needed. The i m a G e P r o x y described in the motivation section is an example of such an agent.
      3) The Protection agent (Protection proxy) Controls access to the original object. The protection agent is used when the object should have different access rights. 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 a simple pointer that performs some additional operations when accessing an object. Typical uses of it include:
    • A reference count that points to the actual object, so that when the object has no references, it can be automatically freed (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 memory.
    • Before accessing an actual object, check to see if it has been locked to ensure that other objects cannot change it.
Iv. Behavioral Type interpreter .    Intentions:Given a language, define a representation of its grammar and define an interpreter that interprets the sentences in the language using that representation.Applicability:
    • You can use the interpreter pattern when there is a language that needs to be interpreted and executed, and you can represent a sentence in that language as an abstract syntax tree. This mode works best when the following conditions are present:
    • The grammar is simple for complex grammars, and the class hierarchy of grammars becomes large and cannot be managed. Tools such as the parser generator are a better choice at this point. They can explain expressions without building an abstract syntax tree, which saves space and can save time.
    • Efficiency is not a key issue the most efficient interpreters are usually not implemented by directly interpreting the parse tree, but rather first converting them to another form. For example, regular expressions are often converted to state machines. However, even in this case, the converter can still be implemented using the interpreter pattern, which is still useful.
Template Method   Intentions:Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Te m P L a t e m e t h o D allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.Applicability:
    • One-time implementation of an invariant part of the algorithm, and the variable behavior is left to subclass to achieve.
    • The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication. This is a good example of the "re-decomposition to generalize" described by O p D y k e and J o h n s o n [o J 9 3]. First identify the differences in the existing code and separate the differences into new operations. Finally, replace these different code with a template method that invokes these new operations.
    • Controls the subclass extension. The template method invokes the "H o O K" operation only at a specific point (see the Effects section) so that only those points are allowed to be extended.
Chain of Responsibility   Intentions:Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Link the objects together and pass the request along the chain until an object handles it.Applicability:
    • There are multiple objects that can handle a request and which object handles that request automatically when the run time is determined.
    • You want to submit a request to one of multiple objects without explicitly specifying the recipient.
    • A collection of objects that can handle a request should be specified dynamically.
  . Command    Intentions:Encapsulates a request as an object so that you can parameterize the customer with different requests, queue requests or log requests, and support undoable operations.Applicability:
    • Abstract 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 registers somewhere before it is called at a later time when it is needed. C o m 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 m a n-D object can have a lifetime that is independent of the initial request. If the recipient of a request can be expressed in an address space-independent manner, the Command object responsible for the request is routed to a different process and the request is implemented there.
    • Support Cancel operation. The e x c u T e operation of C o m m a n D can store the state before the operation is implemented, which is used to eliminate the effect of the operation when the operation is canceled. C o m m a n D interface must be added a U n e x e c U t operation, which cancels the effect of the last E x e c U T-e call. The commands executed are stored in a history list. You can iterate through the list backwards and forwards and invoke the U n e x e c U T e and E x e c U T e respectively to achieve an unlimited number of "undo" and "Redo".
    • Support for modifying the log so that when the system crashes, these modifications can be re-done again. Adding mount operations and storage operations to the C o m a n-D interface can be used to maintain a consistent modification log for changes. The process of recovering from a crash involves re-reading the recorded commands from disk and re-executing them with E x e c U-T e operations.
    • 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 set of changes to the data. C o m A n-d mode provides a way to model transactions. C o m m A n D has a common interface that allows you to invoke all transactions in the same way. It is also easy to add new transactions to extend the system by using this mode.
  Iterator .   Intentions:Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.Applicability:
    • Accesses the contents of an aggregated object without exposing its internal representation.
    • Supports multiple traversal of an aggregated object.
    • Provides a unified interface for traversing different aggregation structures (that is, support for polymorphic iterations).  
  mediator .   Intentions:Encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.Applicability:
    • A set of objects communicates in a well-defined but complex way. The resulting interdependence structure is confusing and difficult to understand.
    • An object that references many other objects and communicates directly with those objects makes it difficult to reuse the object.
    • You want to customize a behavior that is distributed across multiple classes without having to generate too many subclasses.
  Memento .      Intentions:captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state.Applicability:
    • A. You must save the (partial) state of an object at a certain point in time so that it can revert back to its previous state if needed later.
    • B. If an interface is used to get these states directly from other objects, it exposes the implementation details of the object and destroys the object's encapsulation
    • Of
    • 20.
Observer    Intentions:Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.Applicability:     When an abstract model has two facets, one aspect depends on the other.     The two are encapsulated in separate objects so that they can be individually changed and reused.    When a change to an object needs to change other objects at the same time, it is not known how many objects need to be changed. When an object must notify other objects, it cannot assume that other objects are who. In other words, you don't want these objects to be tightly coupled. 21.State   Intent: Allows an object to change its behavior when its internal state changes. The object appears to have modified its class. Applicability:
    • The behavior of an object depends on its state, and it must change its behavior according to state at run time.
    • An operation contains large, multi-branched conditional statements that depend on the state of the object. This state is usually represented by one or more enumeration constants. Typically, there are multiple operations that contain this same conditional structure. S t a T e mode places each conditional branch into a separate class. This allows you to use the object's state as an object based on the object's own situation, which can vary independently of other objects.
strategy .      Intentions:Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.Applicability:
    • Many of the related classes are simply behavior-specific. Policy provides a way to configure a class with one behavior in multiple behaviors.
    • You need to use different variants of an algorithm. For example, you might define algorithms that reflect different spatial/temporal tradeoffs. When these variants are implemented as a class hierarchy of an algorithm [HO87], you can use the policy mode.
    • The algorithm uses data that the customer should not know about. You can use the policy mode to avoid exposing complex, algorithmic-related data structures.
    • A class defines a variety of behaviors, and these behaviors appear as multiple conditional statements in the operation of this class. The related conditional branches are moved into their respective strategy classes in place of these conditional statements.
 23.Visitor    
        • Intent: represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of each element. Applicability:
          • An object structure contains many classes of objects that have different interfaces, and you want to implement some operations that depend on their specific classes for those objects.
          • You need to do many different and unrelated operations on objects in an object structure, and you want to avoid classes that let these actions "pollute" those objects. Vi s i t o r allows you to centralize related operations into one class. When the object structure is shared by many applications, the vi s i t o r mode allows each app to contain only the actions that need to be used.
          • Classes that define the structure of an object rarely change, but it is often necessary to define new operations on this structure. Changing the object structure class requires redefining the interface to all visitors, which can take a significant cost. If the object structure classes often change, it might be better to define them in these classes.  

Design mode + optimistic lock/pessimistic lock

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.