Command mode of grinding design mode-2

Source: Internet
Author: User
ArticleDirectory
    • 3.1 understand the command mode
    • 3.2 parameterized Configuration
3 mode description

3.1 understand the command mode

(1) Key to the command mode
The key to the command mode is to encapsulate the request into an object, that is, a command object, and define a unified interface for executing operations, this command object can be stored, forwarded, recorded, processed, and revoked. The entire command mode is centered around this object.
(2) command mode assembly and calling
In the command mode, there is often a command assembler who uses it to maintain the relationship between the "virtual" Implementation of the command and the real implementation. If it is a super intelligent command, that is to say, the command object is fully implemented without the receiver, it is the degradation of the command mode, the receiver is not required, and naturally the assembler is not required.
The real user is to specify the request content, and then submit the request for triggering. The real user will trigger the command through invoker.
In the actual development process, the client and invoker can be integrated. When using the command mode, the customer first assembles the command object and receiver. After assembly, you can call the command to execute the request.
(3) receiver of command mode
The receiver can be any class and has no special requirements on it. This object knows how to actually execute command operations and is transferred from the implementation class of command.
A recipient object can process multiple commands. There is no agreed correspondence between the receiver and the command. The number, name, function, and command of methods provided by the recipient can be different, as long as the recipient's method can be called to implement the corresponding function of the command.
(4) Smart commands
In the standard command mode, the command implementation class does not actually implement the functions required by the command, and the real function of executing the command is the recipient.
If the Implementation object of a command is intelligent, it can actually implement the functions required by the command without calling the receiver, this is called a Smart Command.
You can also have semi-intelligent commands. The command object knows partial implementation, while others still need to be completed by calling the receiver. That is to say, the command function is completed by the command object and the receiver together.
(5) The requested object is decoupled from the actually implemented object.
It is unknown who processes the request and how it is processed, that is, the object initiating the request is decoupled from the actually implemented object. The object that initiates the request only sends the command, and the rest will not work.
(6) command mode Call Sequence
The process of using the command mode is divided into two stages. one stage is the process of assembling the command object and the receiver object, and the other stage is the process of triggering the invoker call to make the command really run.
Let's take a look at the calling sequence of the assembly process, as shown in Figure 4:

Figure 4 call sequence of the command mode assembly process
Next, let's take a look at the calling sequence when the command is actually executed, as shown in Figure 5:

Figure 5 sequence of invocation in command mode

 

 

 

 

3.2 parameterized Configuration

The parameterized configuration of the command mode refers to the use of different command objects to parameterize the configuration of customer requests.
As described above: if the customer presses a button to determine whether to start or restart the instance, it depends on the specific Button Object of the parameter configuration. 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, it is equivalent to the same request. However, if different button objects are configured for the request, different functions will be executed.
Use this functionCodeImplementation, let's take a look at the parameterized configuration of the command mode.
(1) define the motherboard interface first. Now you want to add a restart button. Therefore, you need to add a method for restarting the motherboard. The sample code is as follows:

 
/*** Main board interface */public interface mainboardapi {/*** the main board has the power-on function */Public void open (); /*** the motherboard has the function of restarting */Public void reset ();}

The interface has changed and the implementation class has to be changed accordingly. Since the implementation of the two Boards is similar, only one example is provided. The sample code is as follows:

/*** The Real-Time implementer of the gigabyte motherboard class and command, in command mode, act as the executor */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 runs properly ...... "); system. out. println ("the machine has been opened normally, please operate");}/*** the real implementation of restarting the machine command */Public void reset () {system. out. println ("The gigabyte motherboard is restarting the machine now, please wait"); system. out. println ("the machine has been turned on normally, please operate ");}}

(2) The command and button should be defined. The command interface has not changed, and the implementation of the original boot command has not changed. However, a new restart command is added. The sample code is as follows:

/*** Restart the implementation of the machine command to implement the command interface, * Hold the real implementation of the restart machine command, call the receiver's method to execute the command */public class resetcommand implements command {/*** to hold the receiver that actually implements the command-motherboard object */private mainboardapi mainboard = NULL; /*** constructor: input the main board object * @ Param mainboard main board object */Public resetcommand (mainboardapi mainboard) {This. mainboard = mainboard;} public void execute. mainboard. reset ();}}

(3) The chassis that holds the command also needs to be modified. Now there are more than one command button, there are two, so you need to add a new restart button in the chassis class, in order to be simple, not made into a set. The sample code is as follows:

/*** The chassis object has a button, holding the command object corresponding to the button */public class box {private command opencommand; Public void setopencommand (command) {This. opencommand = command;} public void openbuttonpressed () {// execute the command opencommand.exe cute ();}/*** restart the machine command object */private command resetcommand; /*** set the host restart command object * @ Param command */Public void setresetcommand (command) {This. resetcommand = command;}/*** is used by the customer to receive and send user requests. This is equivalent to the method in which the restart button is triggered */Public void resetbuttonpressed () {// execute the command resetcommand.exe cute () ;}} (4) by pressing the down button to see how the customer uses the two buttons. The sample code is as follows:

 

Public class client {public static void main (string [] ARGs) {// 1: combines commands with real implementations, which is equivalent to assembling machines, // Insert the connection line of the button on the chassis to the motherboard. Mainboardapi mainboard = new gigamainboard (); // create the startup command opencommand = new opencommand (mainboard); // create the restart command resetcommand = new resetcommand (mainboard ); // 2: Set the corresponding command for the button on the chassis to let the button Know What To Do box = new box (); // configure it correctly first, that is, the power-on button on the power-on command, restart button on the restart command box. setopencommand (opencommand); box. setresetcommand (resetcommand); // 3: Simulate pressing the button on the chassis system. out. println ("correctly configure ------------------------->"); system. out. println (">>> press the boot button >>>"); box. openbuttonpressed (); system. out. println (">>> press restart: >>>"); box. resetbuttonpressed (); // you can configure it incorrectly once. It is a parameterized configuration. // It is the restart command on the boot button, and the restart button on the boot command box. setopencommand (resetcommand); box. setresetcommand (opencommand); // 4: Simulate pressing the system button on the chassis. out. println ("Incorrect configuration ------------------------->"); system. out. println (">>> press the boot button >>>"); box. openbuttonpressed (); system. out. println (">>> press restart: >>>"); box. resetbuttonpressed ();}}

It is interesting to run it. The result is as follows:

 

 

 

 

 

To be continued ......

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.