Behavioral patterns behavior patterns Summary

Source: Internet
Author: User
ArticleDirectory
    • I. general concept
    • Ii. Classification
I. general concept

 Behavioral patterns
Chain of resp. A way of passing a request between a chain of Objects
Command Encapsulate a command request as an object
Interpreter A way to include language elements in a program
Iterator Sequentially access the elements of a collection
Mediator Defines simplified communication between classes
Memento Capture and restore an object's internal state
Observer A way of changing Ying change to a number of classes
State Alter an object's behavior when its State Changes
Strategy Encapsulates an algorithm inside a class
Template Method Defer the exact steps of an algorithm to a subclass
Visitor Defines a new operation to a class without change
Ii. Classification

Based on its features, it is divided into three types:

1. Four models for decoupling sender and receiver:

Command mode:Decouple is the object that calls a command and the object that executes the command. The object that calls the command has no idea about the object that executes the command and the specific method of execution, only the interfaces that are executed are known (through the abstract class interface of command ). You can change the called command at runtime by using different command objects.
Observer mode:It is also called publish-subscribe. In this mode, multiple observers (subscriber subscribers) subscribe to an object. That is to say, when this object changes, all observers should be notified to maintain the data consistency of the observer. In this mode, you can add or delete the observer (subscriber) at any time ).
Mediator mode:Interaction between all objects must be performed through mediator. Each object does not need to know each other, but only needs to know mediator.
Chain of responsibility: decouple the sender and consumer of the command, so that the request is transmitted along the sender to the consumer er

2. encapsulation:

Find and encapsulate the changes. Find out the differences between objects and use these differences as virtual functions of abstract classes, that is, interfaces. For example, the abstract class is a, and the actual classes are A1 and A2.

Object clustering is preferred, rather than class inheritance. Use aggregation to use these objects. If B wants to use a, a pointer of A is used as a member of the class, and then the interface of A is called. In this way, B does not need to know which method A calls the virtual function, which is determined by the actual object type of a at runtime.

Strategy Mode:DifferentAlgorithmAs
State mode:Use different States as

3. Others:

Visitor:In a structure (usually a composite structure), each subclass has a series of operations for this subclass, and these operations often change, the operation (function) in the entire class hierarchy is required) all changes. In this case, the visitor mode is used to make each operation a separate class.

Template Method:The steps of an algorithm are fixed, but some of the steps are implemented based on the specific subclass.
Memento:You need to save the state of an object, but do not want to expose its interface. Use a memento object to store its State and restore its state as appropriate.
Iterator:Traverse a structure (composite structure)
Interpreter:Parse a Language Structure

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.