Command mode, State mode, responsibility chain mode Difference

Source: Internet
Author: User

Command mode: Once set, unified execution.
State mode:
Equivalent if else if else;
Design route: Internal implementation of each State class (equivalent to the condition within the if,else if)
Execution is performed by invoking the context method with state.
Responsibility Chain Mode:
Equivalent to Swich case
Design route: Customer settings, each subclass (case) parameter is the next subclass (case).
When used, the parameters are passed to the execution method of the first subclass of the chain.

————————————————————————————————————

Command mode: To submit multiple commands to only one object that executes the command
The responsibility chain model is the opposite: submitting only one request to multiple objects that can execute the command

The difference between a state pattern and a responsibility chain pattern:
State mode is to let each state object know its next processing object, which is set at compile time;
The individual objects in the responsibility chain pattern do not specify who the next processing object is, only the client is set.

————————————————————————————————————

Responsibility chain Model

The organization of the chain is from the most special to the most general, and there is no guarantee that the request will be returned in any case.

The chain of responsibility isolates the content of what each object in the program can do, that is, the chain of responsibility reduces the coupling between objects, and each object can operate independently. The responsibility chain can also be used to compose the main program's objects and objects that contain other object instances.

Applicable scenarios:

1 several objects with the same method are suitable for executing a program request operation, but it is more appropriate for the object to decide who will complete the operation than to make the decision in the calling code.

2 One of these objects may be best suited for processing requests, but you do not want to select a specific object with some column IF-ELSE statement or switch statement

3 when the program executes, you need to add a new object to the process selection necklace

4 in cases where multiple objects can execute a request, you don't want to put these interacting content in the calling program

Each object in the chain is "autonomous", and the last object determines whether the request is processed by default or discarded




Command mode

The responsibility chain forwards the request along the class chain, while the command mode forwards the request only to a specific object. The command pattern encapsulates a request for a specific operation into an object and gives the object a well-known public interface, which allows the client to generate the request without knowing what the actual operation is doing, or to change the operation without affecting the client program at all.

The effect of the command mode:

The main disadvantage of the command pattern is that it adds small classes that clutter the program, but even if there is a separate click event, the small private method is usually called to complete the specific function. The final result is that private methods are almost the same length as those of our small classes, so there is usually no difference in complexity between building the command class and writing more methods, the main difference being that the small classes generated by the command pattern are easier to understand.

Another major reason for using command design patterns is that they provide a convenient storage method and can complete the undo function. Each command object remembers what has just been done, and when there is an undo request, the amount and memory requirements are not too much to get back to the state you just made.

Command mode, State mode, responsibility Chain mode difference (RPM)

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.