C # command mode

Source: Internet
Author: User

C # command mode
Command mode:

In the scope of object-oriented programming, Command Pattern is a design Pattern that attempts to represent actual actions with objects.

Mode structure:

Command:

Define the command interface and declare the execution method.

ConcreteCommand:

The implementation object of the command interface is a "virtual" implementation. It usually holds the receiver and calls the receiver's function to complete the operation to be executed by the command.

Aggreger:

Recipient, the object that actually executes the command. Any class may become a receiver, as long as it can implement the corresponding functions required by the command.

Invoker:

The command object is required to execute the request. Generally, the command object is held and many command objects can be held. This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object.

Client:

Create a specific command object and set the receiver of the command object. Note that this is not a Client in the general sense, but an assembly of command objects and recipients. It may be better to call this Client an assembler, because the client that actually uses the command triggers the execution from Invoker.

Mode collaboration:

1. The Client creates a ConcreteCommand object and specifies its Receiver object.

2. an Invoker object stores the ConcreteCommand object.

3. The Invoker submits a request by calling the Execute Operation of the Command object. If the command is unrecoverable, ConcreteCommand stores the current status before executing the Execute Operation to cancel the command.

4. The ConcreteCommand object performs operations on the consumer that calls it to execute the request.

Mode Analysis:

1. The essence of the command mode is to encapsulate the command and separate the responsibility for issuing the command from that for executing the command.

2. Each Command is an operation: the Requesting Party sends a request and requires an operation. The receiving party receives the request and performs the operation.

3. The command mode allows the request party to be independent from the receiving Party, so that the requesting party does not have to know the interface of the Receiving Party, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed.

4. The command mode makes the request itself an object, which can be stored and transmitted like other objects.

5. The key to the command mode is to introduce the abstract command interface, and the sender program the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.

Advantages:

1. Reduce the coupling between objects.

2. New commands can be easily added to the system.

3. You can easily design a combined command.

4. Call the same method to implement different functions.

Disadvantages:

Using the command mode may cause some systems to have too many specific command classes.

Because a specific command class needs to be designed for each command, some systems may need a large number of specific command classes, which will affect the use of the command mode.

Applicable environment:

1. The system needs to decouple the request caller and the request receiver so that the caller and the receiver do not directly interact.

2. The system must support undo commands ). The command object can store the status. When the client needs to revoke the effect of the command, it can call the undo method to cancel the effect of the command. The command object can also provide the redo method for the client to implement the command effect again when needed.

3. The system must specify the request at different times and queue the request. A command object and the original request sender can have different lifecycles. It means that the sender of the original request may no longer exist, and the command object itself may still be active. In this case, the receiver of the command can be either local or another address on the network. The command object can be serialized to the receiver.

4. If a system updates all the data messages in the system to the log, you can read the update commands of all the data in the log when the system crashes, re-call the method to execute these commands one by one to restore the data updates made before the system crashes.

5. The system must use the command mode as an alternative to the "CallBack (CallBack)" in the object-oriented system. Callback registers a method and then calls it later.

Instance resolution

TV remote control:

The TV is the receiver of the request, and the remote control is the sender of the request. There are buttons on the remote control. Different buttons correspond to different operations on the TV. The abstract command role is played by a command interface. There are three specific command classes that implement the abstract command interface. These three specific command classes represent three operations respectively: turn on the TV, turn off the TV, and switch between channels. Obviously, the TV remote control is a typical command mode application instance.

In the scope of object-oriented programming, the CommandPattern is a design pattern that attempts to represent actual actions with objects.
/// <Summary> /// Command Execution interface /// </summary> public interface ICommand {// command execution method void Execute ();} /// <summary> /// channel switching command /// </summary> public class CommandChange: ICommand {private TV myTv; private int channel; public CommandChange (TV TV TV, int channel) {myTv = TV; this. channel = channel;} public void Execute () {myTv. changeChannel (channel) ;}/// <summary> /// shutdown command //</summary> public class CommandOff: ICommand {private TV myTv; public CommandOff (TV TV) {myTv = TV;} public void Execute () {myTv. turnOff () ;}/// <summary> // boot command /// </summary> public class CommandOn: ICommand {private TV myTv; public CommandOn (TV TV) {myTv = TV;} public void Execute () {myTv. turnOn () ;}/// <summary> // it can be viewed as a remote control // Invoker object: The command object is required to execute the request, and usually holds the command object, it can hold many command objects. /// This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object. /// </Summary> public class Control {private ICommand onCommand, offCommand, changeChannel; public Control (ICommand on, ICommand off, ICommand channel) {onCommand = on; offCommand = off; changeChannel = channel;} public void TurnOn () {onCommand. execute ();} public void TurnOff () {offCommand. execute ();} public void ChangeChannel () {changeChannel. execute () ;}/// <summary> // command receiver // </summary> public class TV {public int currentChannel = 0; public void turnOn () {MessageBox. show ("The televisino is on. ");} public void turnOff () {MessageBox. show ("The television is off. ");} public void changeChannel (int channel) {this. currentChannel = channel; MessageBox. show ("Now TV channel is" + channel );}}

Related Article

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.