The behavior model of reading notes in Gof design mode (top)

Source: Internet
Author: User

Background

Looking at the previous creation model and the structure model, we have the class and the overall architecture how to let them really coordinate the work this is another problem, today we entered a complex problem-the behavior model, he controls the class and the communication between the class and mutual control. Resolving complex interaction items between classes can be very helpful for decoupling.

Pattern Features

This paper mainly introduces the characteristics and structure of V design pattern, and records its own ideas.

1. CHAIN of RESPONSIBILITY (responsibility chain)-Object Behavior Mode 1.1 intent

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.

1.2 Applicability

Use the responsibility chain under the following conditions:

    • 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.
1.3 Structure diagram

1.4 Effects

The Responsibility chain has the following advantages and disadvantages:

    1. Reduce coupling This mode allows an object to know which other object is handling its request. The object only needs to know that the request is handled "correctly". Neither the receiver nor the sender has clear information about the other, and the object in the chain does not need to know the structure of the chain. As a result, the chain of responsibility simplifies the connection of objects to each other. They only need to maintain a reference to the successor, without having to keep all of its candidate references.
    2. Increased flexibility in assigning responsibilities to Objects (Responsibility) when assigning responsibilities in objects, the chain of responsibilities gives you more flexibility. You can add or change the responsibilities of handling a request by dynamically adding or modifying the chain at run time. You can use this mechanism in conjunction with the inheritance mechanism of static exception handling objects.
    3. Not guaranteed to be accepted since a request does not have a clear recipient, there is no guarantee that it will be processed-the request may not be processed until the end of the chain. A request may also not be processed because the chain is not configured correctly.
1.5 Note

It feels like this is still for a large project framework, in the Android view class event transmission seems to be using this chain of responsibility, the feeling should be combined with the model of the composite model, to form a chain-like structure.

2. Command-object Behavior Mode 2.1 Intent

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.

2.2 Applicability

When you have the following requirements, you can use the command mode:

    1. For a specific action, you can abstract the action to be performed to parameterize an object. You can use the callback (callback) 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. The command pattern is an object-oriented alternative to the callback mechanism.
    2. Specify, arrange, and execute requests at different times. A command 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.
    3. Support Cancel operation. Command's execute operation stores the state before the operation is implemented, which is used to eliminate the effect of the operation when the operation is canceled. The command interface must add an Unexecute action that cancels the effect of the last execute call. The commands executed are stored in a history list. You can iterate through this list backwards and forwards and invoke Unexecute and execute separately to achieve the infinite number of "undo" and "Redo".
    4. 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 command interface can be used to maintain a consistent change log for changes. The process of recovering from a crash involves re-reading the recorded commands from disk and re-executing them with the execute operation.
    5. Construct a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (transaction). A transaction encapsulates a set of changes to the data. The command pattern provides a way to model transactions. command 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.
2.3 Structure diagram

2.4 Effects

Command mode has the following effects:

    1. The command mode decouples the object that invokes the operation from the object that knows how to implement the operation.
    2. command is the first-class object. They can be manipulated and extended like other objects.
    3. You can assemble multiple commands into a single composite command. In general, a compound command is an instance of the composite pattern.
    4. Adding a new command is easy because it doesn't need to change the existing classes.
2.5 Note

Here it is. All treatments are given a place to dispose of values in one location. If complexity can be solved by using composite mode to solve more complicated problems, we should slowly realize his strong.

3. Interpreter (interpreter)-Class behavior Pattern 3.1 intent

Given a language, define a representation of its grammar and define an interpreter that interprets the sentences in the language using that representation.

3.2 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.
3.3 Structure diagram

3.4 Effects

The interpreter mode has the following advantages and disadvantages:

    1. Easy to change and expand grammar because the pattern uses classes to represent grammar rules, you can use inheritance to change or extend the grammar. An existing expression can be incrementally changed, and a new expression can be defined as a variant of an old expression.
    2. It is also easy to implement a grammar that defines the implementation of the classes of individual nodes in the abstract syntax tree broadly similar. These classes are easy to write directly, and they can often be generated automatically by a compiler or parser generator.
    3. Complex grammar difficult to maintain the interpreter pattern defines at least one class for each rule in the grammar (more classes are required to use the grammar rules defined by BNF). Grammars that contain many rules can therefore be difficult to manage and maintain. Other design patterns can be applied to mitigate this problem. But when the grammar is very complex, other techniques such as the parser or compiler generator are more appropriate.
    4. The addition of a new explanation of how the expression is interpreted makes it easy to implement the new expression "calculation". For example, you can define a new action on an expression class to support a graceful printing or type checking of an expression. If you often create new ways to interpret expressions, consider using the visiter pattern to avoid modifying classes that represent grammars.
3.5 Note

Look after a little unclear, but also to continue to study.

4. ITERATOR (iterator)-Object Behavior Mode 4.1 intent

Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation.

4.2 Applicability

The iterator pattern can be used to:

    • 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).
4.3 Structure diagram

4.4 Effects

The iterator pattern has three important functions:
1. It supports traversing a complex aggregation in different ways in a variety of ways. For example, code generation and semantic checking will traverse the parsing tree. Code generation can traverse the parsing tree either in the middle order or in the pre-order. The iterator pattern makes it easy to change the traversal algorithm, using only one instance of a different iterator instead of the original instance. You can also define the subclass of the iterator yourself to support the new traversal.
2. Iterators simplify the aggregation of interfaces with an iterator traversal interface, and the aggregation itself no longer requires a similar traversal interface. This simplifies the interface of the aggregation.
3. On the same aggregation there can be multiple traversal of each iterator to maintain its own traversal state. So you can do multiple traversal at the same time.

4.5 Note

This structure is very strange. The data structure is similar to the C language structure, which is mainly for encapsulation. Let us not know the contents of the inside, because the list has a loop traversal function.

5. Mediator (mediator)-Object Behavior Mode 5.1 Intent

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.

5.2 Applicability

Use the mediator mode in the following situations:

    • 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.
5.3 Structure diagram

5.4 Effects

The broker pattern has the following advantages and disadvantages:

    1. Reduced subclass generation Mediator The behavior that was originally distributed across multiple objects. Changing these behaviors requires only the generation of mediator subclasses. This allows the various colleague classes to be reused.
    2. The decoupling of each colleague mediator is advantageous to the loose coupling between the colleague. You can change and reuse each colleague class and the Mediator class independently.
    3. It simplifies the object protocol to replace many-to-many interactions with a one-to-many interaction between the mediator and the colleague. A one-to-many relationship is easier to understand, maintain, and extend.
    4. It abstracts how objects work together and encapsulates the mediation as a separate concept into an object, allowing you to shift your attention from the behavior of the objects themselves to the interaction between them. This helps to figure out how the objects in a system interact.
    5. It makes the control centralization of the mediator pattern the complexity of the interactions into intermediaries. Because the broker encapsulates the protocol, it can become more complex than either colleague. This may make intermediaries themselves a hard-to-maintain behemoth.
5.5 Note

This thing is actually an information interchange. structure is the information interaction state of the facade model.

Analysis

Later add, this whole summary is too difficult, temporary level is not enough

The behavior model of reading notes in Gof design mode (top)

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.