design mode (i): Command mode

Source: Internet
Author: User
Design principle:

1. The essence of the command pattern is to encapsulate the command, separating the responsibility for issuing the order and the responsibility for executing the order.

2. Each command is an action: the requesting party makes a request, requests an action, the receiving party receives the request, and executes the action.

3. The command pattern allows the requesting party and the receiving party to be independent, so that the requesting party does not have to know the interface of the party receiving the request, not to know how the request was received, and whether the operation was executed, when it was executed, and how it was executed.

4. The command pattern makes the request itself an object that can be stored and passed in the same way as other objects.

5. The key to the command pattern is the introduction of an abstract command interface, and the sender is programmed for an abstract command interface, and only specific commands that implement an abstract command interface can be associated with the receiver.

?

Invoker is the caller of the command, the command defines the interface, Concretecommand is the implementation of the command virtual, receiver is the command really implemented

The client creates a specific command object and sets the recipient of the Command object.

Typical examples:

The TV is the receiver of the request, the remote control is the sender of the request, there are some buttons on the remote control, different buttons correspond to different operation of the TV. The abstract command role is played by a command interface, with three specific command classes implementing an abstract command interface, three specific command classes representing three operations: turning on the TV, turning off the TV, and switching channels. Obviously, the TV remote is a typical application example of command mode.

design mode (i): Command mode

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.