Design Mode (command mode)-behavior mode

Source: Internet
Author: User

The command mode is also a common mode in development. It is not too difficult and relatively simple. Let's write the command mode in detail below.

 

Advantages

1. Reduce the Coupling Degree of the system.

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

3. You can easily design a combined command.

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 from the request receiver so that the caller and the receiver do not directly interact.

2. The system needs to specify the request, queue the request, and execute the request at different times.

3. The system must support undo and redo operations of commands.

4. The system must combine a group of operations to support macro commands.

 

Command mode (command) 1 Scenario problem 1.1 how to start

It is estimated that some friends may find this title very strange. How can I start the computer after the computer is assembled? Is it okay to press the start button? Isn't there any xuanjicang.
For customers who use computers-that is, it is really easy to boot. Press the start button and wait patiently. But after we press the start button? Who will handle it? What should I do? What kind of process has the computer really started for us to use.
First, let's take a look at the computer startup process.

  • When we press the start button, the power supply starts to power the motherboard and other devices.
  • System BIOS (Basic Input/Output System) of the motherboard after power-on
  • The bios of the motherboard looks for the BIOS of other devices, such as the video card, and asks them to perform self-check or initialization.
  • Start to detect CPU, memory, hard disk, optical drive, serial port, parallel port, soft drive, plug-and-play device, etc.
  • BIOS updates escd (extended system configuration data). escd is a means for the bios and the operating system to exchange hardware configuration data.
  • After all the preceding operations are completed, the BIOS will boot the system according to the user configuration, enter the operating system, and wait until the operating system is loaded and initialized, the familiar system logon interface is displayed.
1.2 What should I do

After talking about the process of starting a computer, some friends may wonder, what should I do?
Yes, it seems that this hardware knowledge has nothing to do with you, but if you make a request: please use the software to show the process above, how do you implement it?
First, let's summarize the above process. The main steps are as follows: First load the power, then check the device, then load the system, and then start the computer properly. But who will complete these processes? How to complete?
We cannot allow customers who use computers to do this work. The main board is the main board. How can customers contact the main board? In reality, the button is connected to the motherboard using a connection line, so when the customer presses the button, it is equivalent to sending a command to the motherboard, let the motherboard to complete the subsequent work.
In addition, from the customer's point of view, starting the system means pressing the button. No matter what kind of motherboard is the same, that is to say, the customer just sends the command, who receives the command, who implements the command, and how to implement it, customers do not care about it.

1.3 what's the problem?

Abstract The above question to describe it: the client only wants to issue a command or request, and does not care who the real receiver of the request is or how to implement it, in addition, the actions of the same request can have different request content. Of course, the specific processing functions are different. How can this problem be solved?

2 solution 2.1 command mode to solve

A reasonable solution for solving the above problems is the command mode. So what is the command mode?
(1) command mode definition
Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support auditable operations.

(2) Application Command mode to solve the problem
First, let's look at the actual computer solution.

First, draw a picture to describe how the actual computer handles the problem described above, as shown in 1:

Figure 1 Computer Operations

When the customer presses the button, the button itself does not know how to handle it. Therefore, the customer requests the motherboard through a connection line to allow the motherboard to truly start the machine.
In order to describe the relationship between them, the motherboard is painted out of the chassis. If the connection line is connected to a different motherboard, the motherboard that actually executes the button request will be different, and the customer does not know these changes.
By introducing buttons and connection lines, we can completely decouple the customer who sends the command from the real-time implementer of the command-the main board. The customer always operates the button, and the customer does not care about the things after the button.
A natural solution is to simulate the above solution to solve the problem raised above.
In the command mode, a command interface is defined to constrain all command objects and then provide specific command implementations. Each command implementation object encapsulates a client request, corresponding to the buttons on the chassis, a single chassis can have many buttons, which is equivalent to multiple specific command implementation objects.
In command mode, the command object does not know how to process the command, and the corresponding receiver object will be used to actually execute the command. Like a computer example, the buttons on the chassis do not know how to handle the function, but instead forward the request to the motherboard for the real function, this motherboard is equivalent to the receiver in command mode.
In the command mode, the relationship between the command object and the receiver object is not inherent. An assembly process is required. The client object in the command mode can implement this function. This is equivalent to a computer example where a button on the chassis and a motherboard are available. You also need a connection line to connect the button to the motherboard.
The command mode also provides an invoker object to hold the command object. In the example of a computer, there are multiple buttons in the chassis, which is equivalent to the invoker object in the command mode. In this way, the client in the command mode can use invoker to trigger and require the execution of the corresponding commands, which is equivalent to the actual customer is to press the button on the chassis to operate the computer.

2.2 schema structure and description

The command mode structure 2 is shown below:

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.

2.3 command mode sample code

(1) Let's take a look at the definition of the command interface. The sample code is as follows:

/**

* Command interface: Declares the executed operation

*/

Public interface command {

/**

* Operations corresponding to commands executed

*/

Public void execute ();

}

(2) Let's take a look at the specific command implementation object. The sample code is as follows:

/*** Specific command implementation object */public class concretecommand implements command {/*** holds the corresponding receiver object */private receiver er ER = NULL, the command object can have its own State */private string state;/*** constructor, pass in the corresponding recipient object * @ Param receiver corresponding receiver object */Public concretecommand (receiver er extends ER) {This. extends ER = extends er;} public void execute () {// It usually calls the corresponding method of the recipient object, so that the recipient can truly execute the function of executor er. action ();}}

(3) let's take a look at the implementation of the receiver object. The sample code is as follows:

/**

* Recipient object

*/

Public class extends er {

/**

* Indicates the method to actually execute the corresponding commands

*/

Public void action (){

// Functional code that truly executes command operations

}

}

(4) let's take a look at the invoker object. The sample code is as follows:

/**

* Caller

*/

Public class invoker {

/**

* Holding command objects

*/

Private command = NULL;

/**

* Set the command object held by the caller

* @ Param command object

*/

Public void setcommand (command ){

This. Command = command;

}

/**

* Indicates the method. The command is required to execute the request.

*/

Public void runcommand (){

// Call the execution method of the command object

Command.exe cute ();

}

}

(5) let's take a look at the implementation of the client,Note that this is not a test client in the general sense. The main function is to create a command object and set its receiver. Therefore, no code is called for execution.The sample code is as follows:

Public class client {

/**

* Indicates to create a command object and set its receiver.

*/

Public void assemble (){

// Create a receiver

Worker er worker ER = new worker Er ();

// Create a command object and set its Receiver

Command command = new concretecommand (aggreger );

// Create an invoker and set the command object

Invoker = new invoker ();

Invoker. setcommand (command );

}

}

2.4 use command mode for Example

To use the command mode to implement the example, you must first map all the parts involved in the command mode to the actual example before designing and implementing the program according to the command mode structure. Based on the solution described above, the solution roughly corresponds to the following:

  • The buttons on the chassis are equivalent to the command object.
  • The chassis is equivalent to invoker
  • The motherboard is equivalent to the recipient object.
  • The command object holds a receiver object, which is equivalent to connecting a connection line to the button of the chassis.
  • When the button on the chassis is pressed, the chassis sends the command through the connection line.

The main board class is the place where the power-on function is actually implemented, and the place where commands are actually executed, that is, the "receiver ". The implementation object of the command is actually a "virtual" implementation, just like the connection line. How can it be implemented? It is not just to pass the command to the motherboard connected to the connection line.
Use the command mode to implement the example structure 3:

Figure 3 structure of the example using command mode
Let's take a look at the sample code. It will be clearer.
(1) define the motherboard
According to the previous descriptions, we will find that the main board is used to really execute customer commands or request, and only the main board knows how to implement Customer commands. Therefore, we should abstract the main board first, describe it with an object.
First, define the interface of the motherboard. At least the motherboard will have a method that can be turned on. The sample code is as follows:

/**

* Main board interface

*/

Public interface mainboardapi {

/**

* The main board has the ability to boot.

*/

Public void open ();

}

If the interface is defined, define the implementation class, and define the implementation classes of the two boards. One is the gigabyte motherboard, and the other is the Microstar motherboard. The current implementation is the same, however, the operations on the same command can be different for different mainboards. Since the two implementations are basically the same, the sample code is as follows:

/**

* The Real real-time implementer of the boot command in the gigabyte motherboard class, acts as a recener in command mode

*/

Public class gigamainboard implements mainboardapi {

/**

* Real startup command implementation

*/

Public void open (){

System.Out. Println ("The gigabyte motherboard is currently starting. Please wait ");

System.Out. Println ("power on ......");

System.Out. Println ("device check ......");

System.Out. Println ("loading system ......");

System.Out. Println ("the machine is running properly ......");

System.Out. Println ("the machine has been turned on normally. Please operate ");

}

}

The implementation of the Microstar motherboard is exactly the same as that of this one, but the technology is changed to Microstar.
(2) define the implementation of command interfaces and commands
For the customer, the start is to press the button and do nothing else. Abstract The user's action, which is equivalent to sending a command or request to the customer, so that other customers do not care. To describe the customer's command, a command interface is defined, which has only one method, that is, execution. The sample code is as follows:

/**

* Command interface: Declares the executed operation

*/

Public interface command {

/**

* Operations corresponding to commands executed

*/

Public void execute ();

}

With the command interface, another specific implementation is to simulate the button function on the chassis in reality, because we press the button, however, the button itself does not know how to start the computer, it needs to forward this command to the motherboard, let the motherboard to truly execute the boot function. The sample code is as follows:

/**

* The command interface is implemented for the startup command,

* The real implementation of the startup command is implemented by calling the receiver's method.

*/

Public class opencommand implements command {

/**

* Receiver holding the real implementation command-the main board object

*/

Private mainboardapi mainboard = NULL;

/**

* Constructor: input the motherboard object

* @ Param mainboard main board object

*/

Public opencommand (mainboardapi mainboard ){

This. mainboard = mainboard;

}

 

Public void execute (){

// For the command object, you do not know how to boot it, and the main board object will be transferred

// Enable the motherboard to start up.

This. mainboard. open ();

}

}

Because the customer does not want to deal with the motherboard directly, and the customer does not know what the motherboard is, the customer just wants to press the start button, and the computer starts up normally, that's easy. Even if you change the motherboard, you can press the start button the same way.
In other words: What Should customers do if they want to completely decouple from the motherboard?
This requires an intermediate object to be created between the customer and the motherboard. The command sent by the customer is passed to the intermediate object, and then the intermediate object is used to find the real executor-the motherboard, to complete the work.
Obviously, this intermediate object is the implementation object of the above command. Note: This implementation is actually a virtual implementation, and the real implementation is completed by the motherboard. In this virtual implementation, it is achieved through the function of transferring the motherboard. The motherboard object instance is passed in from the outside.

(3) provide the chassis
The operation button is placed on the chassis, so you need to define the chassis. The sample code is as follows:

/**

* The chassis object contains buttons and the command object corresponding to the buttons.

*/

Public class box {

/**

* Boot command object

*/

Private command opencommand;

/**

* Set the boot command object

* @ Param command indicates the object of the boot command.

*/

Public void setopencommand (command ){

This. opencommand = command;

}

/**

* This method is used by the customer to receive and respond to user requests.

*/

Public void openbuttonpressed (){

// Press the button to execute the command

Opencommand.exe cute ();

}

}

(4) buttons used by the customer
The chassis and motherboard have been abstracted, and the command object has been prepared. The customer wants to use the button to enable the boot function. before using it, the customer's first thing should be to assemble the buttons and the motherboard to form a complete machine.
In real life, the installation engineer is responsible for this part of the work. For the sake of simplicity, it is written directly at the beginning of the client. After the machine is assembled, the customer should place the button object connected to the motherboard on the chassis, waiting for the customer to operate at any time. This process is also described in code. The sample code is as follows:

Public class client {

Public static void main (string [] ARGs ){

// 1: combining commands with real implementations is equivalent to assembling machines,

// Insert the connection line of the button on the chassis to the motherboard.

Mainboardapi mainboard = new gigamainboard ();

Opencommand = new opencommand (mainboard );

// 2: Set the corresponding command for the button on the chassis to let the button know what to do

Box box = new box ();

Box. setopencommand (opencommand );

// 3: Simulate pressing the button on the chassis

Box. openbuttonpressed ();

}

}

Run the command to check the effect. The output is as follows:

The gigabyte motherboard is currently starting. Please wait

Power on ......

Device check ......

Load the system ......

The machine runs properly ......

The machine is enabled normally. Please operate

You can assemble different motherboard implementation classes for the command object, and then test again to see the effect.
In fact, you will find that after the object structure has been assembled, the task is to face the chassis and press the button on the chassis for the real client, that is, the real user, you can execute the command to start the system. This is also true in real life.

(5) Summary
As in the previous example, the customer's boot request is encapsulated into an opencommand object, and the customer's boot operation becomes the method for executing the opencommand object? If there are other command objects, such as resetcommand objects that restart the machine, the customer can use the different command objects to match the actions of pressing the button, that is, parameterization of the customer.
In the vernacular, the customer presses a button to determine whether to start or restart the instance. It depends on the specific Button Object configured in parameterization. If the parameter is the command object of the instance, execute the boot function. If the parameterized command object is the restart command object, execute the restart function. Although the same button is pressed, the request is different and the corresponding execution function is different.
In the mode description, we will give you an example of parameterized configuration, which will not be discussed here. The queuing or logging of requests and the support for unrecoverable operations are also included in the mode description.

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.