"Onlookers" design mode (31)--Summary of behavioral design patterns (template, observer, strategy, status, responsibility Chain, command, visitor, mediator, memo, interpreter)

Source: Internet
Author: User

design mode source code download

Design mode source code download


1 Template Method mode The Template method pattern defines the steps of an algorithm and allows a subcategory to provide its practice in one or more steps. Let the subcategory redefine some of the steps in the algorithm without changing the algorithm schema. ----WIKIPEDIA

Personal understanding

Template method mode is relatively simple, generally is defined by the abstract class template method, and then, the subclass through the inheritance and implementation of its parent class in a defined template needs to execute the specific method, when invoking the template method of the subclass object, the implementation of the method of the specific implementation of the class. This mode I personally feel a bit like a process-oriented operation, the completion of a process, followed by the next process.

"Onlookers" design pattern (18)--Behavioral Template Method mode (Templatemethod pattern)


2 Observer Patterns The Observer pattern is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This mode is often used for real-time event processing systems. ----WIKIPEDIA
Personal understanding
The observer pattern is to make the object instance of the observer in the Observer, in the event of some events, through the Notify "notify" the Observer, to complete the corresponding operation, he also called the Publish-subscribe pattern, defines a pair of multiple dependencies between objects, so that the object of the observer to generate action, You can notify the object that it depends on. The observed person has changed. "Onlookers" design pattern (19)--Behavioral observer pattern (Observer pattern)

3 Policy Mode as a kind of software design pattern, the policy mode refers to the object has some behavior, but in different scenes, the behavior has different implementation algorithms. For example, everyone has to "pay personal income tax", but "in the United States to pay personal income tax" and "in China to pay personal income tax" there are different methods of tax calculation. ----WIKIPEDIA

Personal understanding
The strategy model can be seen from the name, there are a variety of options, different strategies correspond to different implementations, then the general form for an interface to adopt a variety of implementations (that is, to provide a different strategy), and then provide a choice of a policy class can be defined: Define a set of algorithms, each algorithm is encapsulated together, and make it possible for them to switch between each other. By the way I mentioned above, a set of algorithms refers to a variety of implementation methods, and each algorithm is encapsulated and so that they can be interchangeable, the implementation of a unified interface, naturally can be interchanged.
"Onlookers" design mode (20)--Behavioral Strategy mode (strategy pattern)

4 State mode State mode--allows an object to change its behavior when its internal state changes. The object appears to have modified its class. ----Baidu Encyclopedia

Personal understanding

State mode should be understood as a certain state, the program's execution process may change, similar to traffic lights, when the red light stop, green walk, yellow light, etc., this is the three states of the corresponding changes we have made. For example, the bus should have been sitting, bus parking time, you can get on and off the bus, when the buses are not allowed to get off and on the train, then, here bus parking and driving is two states, these two States for the subsequent people get off the behavior of the car has a certain impact. The popular point is that, when a state is a prerequisite, the latter behavior is affected by the previous state, which is more appropriate to use state mode.

"Onlookers" design mode (21)--Behavioral mode (state pattern)



5 Responsibility Chain mode The model of responsibility chain is a kind of software design pattern in object-oriented programming design, which contains some command objects and a series of processing objects. Each processing object determines which command objects it can handle, and it knows how to pass a Command object that it cannot handle to the next processing object in the chain. The pattern also describes how to add a new processing object to the end of the processing chain. ----WIKIPEDIA

A personal understanding

Chain of responsibility model used to the data structure of the list, there is a certain order, a->b->c such a chain list, in the chain of responsibility mode, the request to a for processing, if a can not be handed over to b,b if the processing of processing, otherwise to C processing, The key to the pattern is to build such a list, and to complete the switching of these processes between the lists, which in the definition should say that you can pass an unhandled command object to the next processing object in the chain. So it seems that our usual layered structure can not be understood as a non-pure responsibility chain model? But we may have to do something for each layer in our layered structure, so it is not consistent with the chain of responsibility, because in the chain of responsibility is the whole ' Responsibility ' is pushed to the next node, but in a sense it is somewhat similar, because it is also my personal understanding and thinking that readers have different opinions to comment on.
"Onlookers" design mode (22)--Behavioral model of responsibility chain (Chain of Responsibility pattern)


6 Command mode In the context of object-oriented programming, the command pattern is a design pattern that attempts to represent actual actions with objects. The command object encapsulates the action and its arguments so that these actions can be:

Repeat multiple times
Cancel (if the object has an implementation)
Cancel and redo again

These are the necessary features of modern, large-scale applications, namely "resiliency" and "repetition". ----WIKIPEDIA

Personal understanding
The command pattern is a high-cohesion pattern, which encapsulates a request as an object, allowing you to parameterize the client using different requests, to be widely used in the project, to separate the requester from the receiver, and to be more extensible. There are three main characters in the command pattern, one is the receiver of the command, and he is responsible for responding to the command received. One is the command role, and he is responsible for defining what commands the recipient needs to execute. The other is the caller (Invoker), which receives the command call and executes the command. But the command pattern also exists insufficient, if the command more time there will be multiple subclasses, resulting in a bloated, can be combined with other patterns of design, such as the combination of responsibility chain mode to achieve the resolution of the command, combined with template method to reduce the expansion of sub-class. "Onlookers" design mode (23)--Behavioral mode (command pattern)

7 Visitor Mode Visitor Pattern: 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 individual element classes.

Personal understanding

Visitor mode, the ability to expand very well, in line with the opening and closing principle, open for expansion, the modification is closed. However, from the realization of the class, the visitor class and the element class depend on each other, the dependency relationship is strong, but can be transferred to the upper class or interface through the form of abstract class or interface, thus reducing the dependency on the implementation class. The starting point of the visitor pattern: business rules require that multiple different objects be traversed. The visitor pattern is an extension of the iterator pattern, and he can access different objects for the purpose of traversal.

"Onlookers" design mode (24)--Behavioral visitor pattern (Visitor pattern)



8 Broker Mode Encapsulating a series of object interactions with an object, the mediator makes the object do not need to display the interaction, so that it is loosely coupled, and can independently change their independence.

Personal understanding
When there is too much coupling between multiple objects, it can be decoupled by the mediator mode, and the coupling between the concrete objects is transferred to the coupling between the mediator and the concrete object, if the former is the coupling between the three objects and the coupling between the intermediary and the concrete class, the coupling is greatly reduced, If it is modified again, then the change is mainly in the intermediary part, which makes the structure more stable.

Role analysis
The mediator role is divided into the following sections:
1. Mediator Abstract Mediator role: The abstract mediator role defines a unified interface for communication between colleague roles.
2. Concrete mediator specific Mediator role: the role of specific intermediaries to achieve writing by coordinating the roles of colleagues, relying on the role of each colleague.
3. Colleague co-worker role: Each colleague class of tasks, including their own needs to complete the function, and do not have to hand over to the intermediary to do the rest of the processing of some of the functions.
"Onlookers" design pattern (25)--Behavioral type of Mediator mode (mediator pattern)

9 Memo Mode The so-called memo pattern is to capture the internal state of an object without destroying the package, and to save the state outside of the object, so that the object can be restored to its previously saved state at a later time.

Personal understanding
Memo mode is a pattern that can be used to temporarily restore an object's state in some special cases, and can be implemented in a variety of ways, including clones and general methods, as well as memos of various parameters. The standard memo is difficult to apply directly in the project, most of it is after the deformation of the processing mode.

The memo pattern mainly includes the following roles:
Originator: the original generator. is responsible for creating a memo that records the internal state of the current object, which can also be used to restore the internal state using the memo. At the same time, the original generator can also decide the internal state of memento storage originator as needed.
Memento: Memo. Used to store the internal state of the originator, and can prevent objects other than originator from accessing the memento. There are two interfaces in memo memento, where caretaker can only see the narrow interface in the memo, and it can only pass the memo to other objects. Originator can see a wide interface, allowing it to access all data returned to its previous state.
Caretaker: the person in charge. Be responsible for saving memos, not manipulating and accessing the contents of the memo, but only passing notes to other objects.
"Onlookers" design mode (26)--Behavioral Memo mode (Memento pattern)

10 Interpreter Mode The parser is an example of parsing in accordance with the prescribed syntax, which is used less in the current project, defined as follows: Given a language, define a representation of its grammar, and define an interpreter that interprets the sentences in the language.

Personal understanding

The interpreter pattern is seldom used in the project because he can cause problems such as efficiency, performance, and maintenance, and when preparing to use the schema, open source frameworks such as expression4j, MESP, Jep, etc. are considered. The interpreter pattern is commonly used to parse a standard set of characters, such as SQL syntax analysis.

Interpreter role
The interpreter pattern consists mainly of the following roles:
Abstractexpression: An abstract expression. Declares an abstract interpretation operation that is shared by all nodes in the abstract syntax tree.
Terminalexpression: an terminator expression. Implements interpretation operations related to Terminator in the grammar. Implements the method required in an abstract expression. Every terminator in grammar has a specific ending expression corresponding to it.
Nonterminalexpression: Non-terminator expression. A non-terminator-related interpretation operation in the grammar.
Context: Environment class. Contains some global information outside of the interpreter.
Client: Customer class.
Abstract syntax tree describes how to form a complex sentence, through the analysis of the abstract syntax tree, can identify the language of the Terminator and non-Terminator class. In the interpreter mode, because each terminator expression, non-terminator expression will have a specific instance corresponding to it, so the scalability of the system is better.
"Onlookers" design mode (27)--Behavioral interpreter mode (interpreter pattern)

"Onlookers" design mode (31)--Summary of behavioral design patterns (template, observer, strategy, status, responsibility Chain, command, visitor, mediator, memo, interpreter)

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.