. The design pattern in net four: Command mode

Source: Internet
Author: User
Tags define continue execution object serialization require serialization thread
The design command structure is not complex, in short, to encapsulate certain responsibilities into objects, and the object's Invoker and receiver (reciever) do not interfere with the execution of the command. In this way, the coupling between the excitation and the recipient is solved, providing a clearer hierarchy for the program. In practice, the commands in the system are often abstracted into an interface, and all commands are implementations of this interface, as follows:


The command class is an implementation of the ICommand, the command class implements the Execute method, and the other command classes are subclasses of the command method. The environment that the motivational recipient and the recipient faces is very simple: unification can be handled as a command type.

The simplest implementation code for the command pattern is as follows:

Define Command interface

public interface ICommand

{

void execute ();

}



Implement ICommand interface

The command class is the base class for all commands, where you implement a common process for some commands

Public abstract class Command:icommand

{

Public virtual void execute ()

{

}

Implement other methods ....

}



To achieve a specific Command1

public class Command1:command

{

public void override Execute ()

{

The specific processing process

}

}



To achieve a specific Command2

public class Command2:command

{

public void override Execute ()

{

The specific processing process

}

}



To achieve a specific Command3

public class Command3:command

{

public void override Execute ()

{

The specific processing process

}

}


Practical Application of Command mode

Let's look at the application of command mode from a system instance. Reportingservice is part of a reporting system, a service program running on a Windows system that requires the ability to generate hundreds of of reports of large amounts of data in a database every night from 11 o'clock to 2 o'clock in the morning. The distribution of the report is more complicated, some require to publish the report on the Web server, some require to output the report data into a text file, and some should save the report in the database system.

The design of the system uses the command mode, and part of the structure is as follows:



The ICommand interface is defined in the program, and several command classes are implemented (only 3 representations are shown here) to perform the build tasks for each report individually. The command class provides the following interfaces:

Description of category name type
Attribute finished bool Indicates whether the task has been completed
Attribute context Iapplicationcontext the environment and resources required for task execution, including database connections, file systems, and so on
Method execute void to perform a task

The main thread of the service initializes the system's operating environment on a daily basis, creating an instance of the ApplicationContext class (application is a Singlton-mode class, not reflected in the diagram), This class holds all the resources required for command operation. It then initializes a list of command objects, executing each command individually. As a result, complex business logic is separated from the main framework of the program, the main thread of the service need to do is to coordinate the allocation of resources and exception processing, but also can constantly check the status of command execution, to see if the finished attribute is true. If you find that some of the commands do not succeed after execution, do the appropriate processing.

The program uses the development method of unit test, when the main thread of the program is tested, a virtual command implementation is used. When testing each command class, a virtual ApplicationContext is implemented for each command. Such a structure provides a lot of convenience for unit testing. To improve the testability of your program, see my other article: How to test the hard parts of your code.

Combination of command mode and other patterns

In order to provide a neat and simple way to build command, the command pattern is often used in conjunction with Factory mode (Factory), using a factory to manage the Command object's establishment, reducing the degree of coupling between commands and command objects.

In a form program, Command mode is often used in conjunction with the combination mode (composition). When you initialize a form, you can combine the menu items, toolbars, buttons, and other controls on the screen with the corresponding command, so that when the control clicks, the corresponding command is invoked to enable processing of the commands.

In the 10th month of this year, the programmer magazine introduced an article that uses command mode to implement the Undo function in a program. Two actions, "execute" and "undo", are defined for each command in the program, as follows:



The action in the diagram is the Commamnd object, except that the name is different and the meaning is the same. The action object exists the Do and undo methods, respectively, the "execute" and "undo" methods. You can set the Done property of the completed action object to True, and then save it in a list of--actionlist. A "undo" action can be implemented by finding the last action in ActionList that is true and executing its Undo method.

Serialization of command classes

If you serialize the command base class and every subclass of it, you can achieve more powerful functionality. During program execution, if an exception occurs, such as network interruption, file loss, and so on, you can save the command serialization without execution. Wait for the time to be executed and reload the objects, and the command can continue. Even if the user has restarted the computer, the command can continue to execute. The serialization of command objects also facilitates distributed operations. In a network environment, a command object can be created somewhere, serialized, and then transferred to another computer on the network for execution. In this case, do not forget the serialization of the exception class, and if you define the subclass of the exception, serialize it to capture and handle the exception that occurred during command execution in the network environment. An article on the Microsoft Web site describes the serialization of objects in. NET, with a detailed introduction to the concept of object serialization.


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.