Design mode four in .NET: Command mode

xiaoxiao2021-03-06  80

Design mode four in .NET: Command mode

Source: Unknown Author: Unknown Recommended rating: Visits: 37

Command mode is not complicated, simply, is to encapsulate some responsibilities into objects, and the object's exciteor and recide are not asking the execution of the command. In this way, the coupling between the excitation and the recipient is released to provide a clearer hierarchy for the program. The actual application is often abstracts the commands in the system into an interface. All commands are implemented in this interface, as shown below: The Command class is the implementation of ICommand, the Command class implements the Execute method, and the other command classes are the Command method. Subclass. Such an environment facing the excitation recipient and the recipient is very simple: unified as a Command type can be handled. The implementation code of the simplest command mode is as follows: // Define Command Interface Public Interface ICOMMAND {void Execute ();} // Immation ICOMMAND Interface // COMMAND class is the base class of all commands, here to implement some commands Common Process Public Abstract Class Command: iCommand {Public Virtual Void Execute () {} // Implementing other methods ....} // Immand 1: Command1 Public Class Command1: Command {Public Void Override Execute () { // Specific processing}}} // Immand2 public class command2: command {public void override execute () {// Specific processing}} // Immand3 public class Command3: CommAND {Public Void Override Execute () {// Specific Processing Procedure}} The actual application of the command mode takes a look at the application of command mode from a system instance. ReportingService is part of a report system that is a Service program running on a Windows system, and the functions you need are: Every evening 11pm to 2 am, generate hundreds of reports in the database. The distribution of the report is more complicated. Some requirements are published on the web server, and some requires outputting the report data into a text file, and there is still to save the report in the database system. The system design uses command mode, part of which is as follows: The ICommand interface is defined in the program, which implements multiple Command classes (here only 3 representations), and performs the generation tasks of each report. The COMMAND class provides the following interfaces: Category Name Type Description Property Finished Bool Indicates whether the task has performed the environment and resources required for the Context IapplicationContext task, including database connections, file systems, etc. Execute Void executes the main thread of the task service. Initialize the system running environment, create an instance of an ApplicationContext class (Application is a class of Singlton mode, no reflection in the figure), which saves all resources that Command to run in the COMMAND. Then initialize a list of Command objects, perform each Command one by one. In this way, the complex business logic is separated from the main frame of the program, and all the work that Service's main thread needs to do is to coordinate resource allocation and abnormal processing, and you can constantly check the status execution of the command, see The finished property is true.

转载请注明原文地址:https://www.9cbs.com/read-70461.html

New Post(0)