Design Pattern-Behavior Pattern Model

Source: Internet
Author: User

Observer)

The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can update themselves automatically.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release + C7pdCt1/release + C52LbUz/release + G4 + release/M5dPQtuDJ2bbUz/release/Eo8q9o7s8L3A + release/release +/ eo8q9y/authorization + zcrHveKz/fHuus + signature/M5aGjtNO2 + signature/Signature + 7bUz/Signature + 7bUz/Signature + signature + b7dx + mxqLXEsrvNrNf2s/expires + signature + l2r0rvQqbK719/release/Wy/y1xNPFysahozwvcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140518/20140518093034271.png" alt = "\">

Code repetition in programming is a common phenomenon. If we see repeated code in more than one place, we should try to combine them into one, and the program will become better. Subtle repetition occurs in different but essentially identical structures or steps.

Comment: Think about our exam questions. We can't repeat the tests of millions of candidates nationwide for several million times. The importance of inheritance.

Command)

Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations.

The object fish that calls the operation knows how to implement the object decoupling of the operation, which means that we can do a lot of things between the two, such as specifying at different times, arranges and executes requests and supports undo and redo operations.

Comment: division of labor and cooperation, efficiency is the best, do a single responsibility.

Command mode VS intermediary mode VS observer Mode: All three use the intermediary to communicate between classes. The intermediary mode is to solve the interaction between many classes, the command mode decouples the object that calls the operation from the object that guides you how to implement the operation. When the status of the theme object changes, it notifies all the observer objects so that they can automatically update themselves. The intermediary is only for the sake of simplicity and convenience of interaction, and the command mode can indeed record the logs of the entire operation, so as to find out the cause when system problems occur in the future and support transactions. The observer mode is a one-to-many decoupling method, so you do not need to know how much it is.

Chain of ResPonsiblity)

So that multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the request sender and the receiver, connect the object into a chain, and pass the request along this chain, until an object is processed.

Just like we deliver a parcel, We can't directly deliver the parcel to the courier of SF Express, but via a dot, and then he is giving it to the Head Office. The head office is distributing it according to the conditions and processing it at the first level, at last, we can go to SF Express's little brother. However, we do not need to know the procedures we have gone through. We just need to deliver.

Comments: Do a Good Job of single responsibility principle, but be careful when writing an incorrect email address, no one will receive it.

State)

When the internal state of an object changes, it is allowed to change its behavior. This object seems to have changed its class. Put specific status-related behaviors in an object. Because all status-related code is stored in a specific status class, therefore, by defining a new subclass, you can easily add new States and transformations.

Because the status change behavior also changes with the status change, we need to separate the status so that maintenance can be used more.

Comment: The open-closed principle is true and ubiquitous.

Responsibility Chain Mode VS status mode:Both modes appear only when the program needs to make judgments. The responsibility chain requires an order of choice, while the state mode does not need a specific order, however, we know that the program is constantly changing, and the change is not changing. Therefore, we need to encapsulate these branches in the subclass separately, in this way, we can dynamically add or modify the conditions we need to select. Therefore, If or swith is no longer required.

Interpreter mode (interpreter)

Given a language, it defines a syntax expression and an interpreter that uses this representation to interpret sentences in the language. It is easy to expand and change grammar,


If a specific type of problem occurs frequently enough, you can consider expressing each instance of the problem as a sentence in a simple language, that is, by building an interpreter, this explains these sentences to solve the problem. In this way, you do not need to construct a specific algorithm for the string mode.

Comments: professional things, we will use professional tools to solve, simple, fast.

Mediator)

An intermediary object is used to encapsulate the interaction of interaction objects of some columns. The intermediary makes it unnecessary for each object to explicitly reference each other, so that the coupling is loose, in addition, the interaction between them can be changed independently. It is generally used in a group of places where objects are well defined, but communication is complex.


Because we advocate a single responsibility principle in object-oriented systems, many classes may exist in a project, in this way, the interaction between the two classes will lead to Spider-web disorder, which is very unfavorable for maintenance. In this case, we can establish an intermediary who can use the intermediary to interact between classes to bridge the interaction between classes.

Comment: Is our operating system an intermediary mode.

Visitor mode (Visitor)

An operation that acts on each element in an object structure. It allows you to operate on these elements without changing the classes of each element.


Elements are stable, but changes in the state of elements can indeed change. elements are stable, but operations that depend on the construction of complex object structures become very easy, you only need to add a new visitor to add a new operation to the structure of an object.

Comments: The structure is stable enough, and the operation changes a lot to make it easy to use.

Strategy)

It is used to encapsulate almost any type of rules. As long as there are business rules that need to be different at different times in the analysis process, you can use a policy model, such as discounts and points for a mall.


Inheritance provides A method that supports multiple algorithms or actions. We can directly generate A subclass of Class A, Class B, Class C, and Class D to give them different actions, however, this will harden the behavior into A, which is very unfavorable for the maintenance of A. However, after careful analysis, we will find that they only have different algorithms or behaviors, encapsulate algorithms in an independent Strategy class so that you can change other classes independently of Class A so that they are switched, easy to understand, and easy to expand, this obviously means that the combination is better than inheritance.

Comments: combine them first, and inherit from them.

Memorandum mode Memeton

The memorandum mode captures the internal state of an object without compromising encapsulation, and saves the State outside the object, in this way, the object can be restored to the previously saved state.


This avoids exposing some information that must be managed by object A but must exist outside object A. The memorandum mode shields the internal information of object A, which may be very complex, from other objects, therefore, the encapsulation boundary is maintained.

Rating: Do you still remember the games we used in that year?


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.