1. Scenario Simulation
Use software to simulate the Boot Process
Press start.
Then Power Supply
The motherboard starts power-on self-check.
BiOS searches for the BIOS of other devices in turn and asks them to initialize self-check
Start to detect CPU, memory, optical disc, hard disk, optical drive, serial port, parallel port, and out-of-the-box drive
Go to system boot
The preceding process can be abstracted as follows:
The client only wants to send commands or requests. It does not care who the real receiver of the request is or how it is implemented. Moreover, the actions of the same request can have different request content, of course, the specific processing functions are different. How can this problem be achieved?
2. Use the command mode to Solve the Problem 2.1 define the command mode
Encapsulate a request as an object. Instead, you can use different requests to parameterize the customer, queue requests or request logs, and support auditable operations.
2.2 structure of command mode
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 a corresponding recipient object will be used to actually execute the command.
3. Command mode sample code details 3.1 command interface, declare the executed operation
Package demo12.command. example1;/*** command interface, declare the executed operation */public interface command {/*** execute the corresponding operation */Public void execute ();}
3.2 specific command implementation object
Package demo12.command. example1;/*** specific command implementation object */public class concretecommand implements command {/*** holds the corresponding recipient object */private receiver er extends ER = NULL; /*** indicates that the command object can have its own State */private string state;/*** constructor, input the corresponding recipient object * @ Param aggreger corresponding recipient 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. action ();}}
3.3 recipient object
Package demo12.command. example1;/*** receiver object */public class extends er {/*** to indicate a method to truly execute the corresponding operation of the command */Public void action () {// functional code for Executing command operations }}
3.4 caller object
Package demo12.command. example1;/*** caller */public class invoker {/*** holding command object */private command = NULL; /*** set the command object held by the caller * @ Param command object */Public void setcommand (command) {This. command = command;}/*** command to invoke the method. The command execution request */Public void runcommand () {// Execute Command command.exe cute ();}}
3.5 client usage
Package demo12.command. example1; public class client {/*** represents, which is used to create a command object and set its receiver */Public void assemble () {// create the receiver's receiver ER = new receiver Er (); // create a command object and set its receiver's command = new concretecommand (receiver ER); // create invoker, set the command object to invoker = new invoker (); invoker. setcommand (command );}}
4. Use the command mode to implement the problem 4.1 sample code structure
The buttons on the chassis are equivalent to the command object.
The chassis is equivalent to the caller
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.
4.2 define the motherboard Interface
Package demo12.command. example2;/*** main board interface */public interface mainboardapi {/*** the main board has the power-on function */Public void open ();}
4.3 gigabyte Motherboard
Package demo12.command. example2;/*** the Real-Time implementer of the power-on command for the gigabyte motherboard, 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 turned on normally, please operate ");}}
4.4 command INTERFACE
Package demo12.command. example2;/*** command interface, declare the executed operation */public interface command {/*** execute the corresponding operation */Public void execute ();}
4.5 specific implementation of commands
Package demo12.command. example2;/*** The Implementation of the boot command to implement the command interface. * The real implementation of the boot command is held, call the receiver's method to execute the command */public class opencommand 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 opencommand (mainboardapi mainboard) {This. mainboard = mainboard;} public void execute () {// for command objects, do not know how to boot, the main board object will be redeployed // Let the Main Board to complete the boot function this. mainboard. open ();}}
4.6 provide Chassis
Package demo12.command. example2;/*** the chassis object with buttons. Hold the command object */public class box {/*** startup command object */private command opencommand; /*** set the boot command object * @ Param command the boot command object */Public void setopencommand (command) {This. opencommand = command;}/*** is provided to the customer to accept and request the user. It is equivalent to the method triggered by the button being pressed */Public void openbuttonpressed () {// Execute Command opencommand.exe cute () ;}} by pressing the down button ();}}
4.7 client usage
Package demo12.command. example2; public class client {public static void main (string [] ARGs) {// 1: combines commands with real implementations, 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 buttons on the chassis, let the button Know What To Do box = new box (); box. setopencommand (opencommand); // 3: Simulate pressing the box on the chassis. openbuttonpressed ();}}
5. Explain the key of command mode 5.1
Is to encapsulate the request as an object.
In command mode, there is usually a command assembler that maintains the relationship between virtual implementations and real implementations.
In addition, the object initiating the request is decoupled from the actually implemented object.
5.2 The following figure shows the call sequence of the command mode:
6. parameterized configuration of commands
Different command objects can be used to parameterize customer requests.
It is actually quite simple. For example, add a restart function.
Then add a reset () function on mainboardapi, and then re-write a resetcommand to implement the command. Of course, add a restart function resetbuttonpressed () in the box. The client call is as follows:
Package demo12.command. example3; public class client {public static void main (string [] ARGs) {// 1: combines commands with real implementations, 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 ();}}
7. Undo operations (important)
The operation that can be undone means to discard the operation and return to the status where the operation is not performed.
There are two basic ideas:
1. Retry, also known as reverse operation.
Enable and disable addition/Subtraction
2. Storage recovery
Record the status before the operation.
8. Retry, also known as reverse operation (taking the calculator as an example) 8.1 operation interface (equivalent to the motherboard Interface)
Package demo12.command. example4; /*** operation interface */public interface operationapi {/*** obtain the calculated result * @ return the calculated result */Public int getresult (); /*** set the initial value of calculation start * @ Param result calculate the initial value */Public void setresult (INT result ); /*** execute addition * @ Param num the number to be added */Public void add (INT num ); /*** run the subtraction operation * @ Param num to subtract the number */Public void substract (INT num );}
8.2 actual operation implementation class (equivalent to the Main Board)
Package demo12.command. example4;/*** operation class, which truly implements addition and subtraction operations */public class operation implements operationapi {/*** records the operation result */private int result; Public int getresult () {return result;} public void setresult (INT result) {This. result = result;} public void add (INT num) {// implement addition function result + = num;} public void substract (INT num) {// implement the subtraction function result-= num ;}}
8.3 abstract command INTERFACE
Package demo12.command. example4;/*** command interface that declares the executed operation, supports undo operations */public interface command {/*** operations corresponding to command execution */Public void execute (); /*** the operation corresponding to the execution of the undo command */Public void undo ();}
8.4 real implementation command class (add and subtract commands)
Package demo12.command. example4;/*** specific addition command implementation object */public class addcommand implements command {/*** holding the object for specific computing execution */private operationapi operation = NULL; /*** operation data, that is, the data to be added */private int openum;/*** constructor, input the specific computing object * @ Param operation the specific computing object * @ Param openum the data to be added */Public addcommand (operationapi operation, int openum) {This. operation = operation; this. openum = openum;} public void execute () {// call the receiver to actually execute the function. This command is used to add this. operation. add (openum);} public void undo () {// transfer the receiver to actually execute the function // The command itself is used for addition, so this is subtracted when the command is revoked. operation. substract (openum) ;}} package demo12.command. example4;/*** specific subtraction command implementation object */public class substractcommand implements command {/*** holding the object for specific computing execution */private operationapi operation = NULL; /*** operation data, that is, the data to be subtracted */private int openum;/*** constructor, input the specific computing object * @ Param operation the specific computing object * @ Param openum the data to be subtracted */Public substractcommand (operationapi operation, int openum) {This. operation = operation; this. openum = openum;} public void execute () {// call the receiver to actually execute the function. This command is used to subtract this. operation. substract (openum);} public void undo () {// call the receiver to actually execute the function. // The command itself is used for subtraction, so this is added when the receiver is revoked. operation. add (openum );}}
8.5 calculator class (equivalent to chassis)
Package demo12.command. example4; import Java. util. *;/*** calculator class, which includes the Add button and subtract button on the calculator, there are also operation history records of the Undo and recovery buttons */public class calculator {/*** command, use the */private list <command> undocmds = new arraylist <command> ();/*** command to undo the history record, */private list <command> redocmds = new arraylist <command> (); Private command addcmd = NULL; private command substractcmd = NULL; Public void setaddcmd (command addcmd) {This. addcmd = addcmd;} public void setsubstractcmd (command substractcmd) {This. substractcmd = substractcmd;} public void addpressed(%%%this.add%.exe cute (); // record the operation to undocmds in the history. add (this. addcmd);} public void substractpressed({%this.substract;.exe cute (); // record the operation to undocmds in the history. add (this. substractcmd);} public void undopressed () {If (this. undocmds. size ()> 0) {// retrieve the last command to undo command cmd = This. undocmds. get (this. undocmds. size ()-1); cmd. undo (); // if restoration is available, record this command to the restoration history. This. redocmds. add (CMD); // then delete the last command, this. undocmds. remove (CMD);} else {system. out. println ("sorry, no command can be undone") ;}} public void redopressed () {If (this. redocmds. size ()> 0) {// retrieve the last command to redo command cmd = this.redocmds.get(this.redocmds.size(2017-100000000.exe cute (); // record this command to the history record that can be undone this. undocmds. add (CMD); // then delete the last command this. redocmds. remove (CMD);} else {system. out. println ("sorry, no recoverable command ");}}}
8.6 client usage
Package demo12.command. example4; public class client {public static void main (string [] ARGs) {// 1: assemble the command and the receiver // create the receiver operationapi operation = new operation (); // create a command object and assemble the command and the receiver addcommand addcmd = new addcommand (operation, 5); substractcommand substractcmd = new substractcommand (operation, 3); // 2: set the command to the owner, that is, Calculator calculator = new calculator (); calculator in the calculator. setaddcmd (addcmd); calculator. setsubstractcmd (substractcmd); // 3: Click the simulate button to test calculator. addpressed (); system. out. println ("the result after an addition operation is:" + operation. getresult (); calculator. substractpressed (); system. out. println ("the result after a subtraction operation is:" + operation. getresult (); // test undo calculator. undopressed (); system. out. println ("the result after cancellation is:" + operation. getresult (); calculator. undopressed (); system. out. println ("the result after the Undo is:" + operation. getresult (); // Test restoration calculator. redopressed (); system. out. println ("the result after one recovery operation is:" + operation. getresult (); calculator. redopressed (); system. out. println ("the result after the restore operation is:" + operation. getresult ());}}
9. Storage recovery
Let's talk about several concepts:
Macro command: a set of commands for multiple commands
Queue request: queues command objects to form a work queue, and extracts command objects for execution in sequence.
The main point of the storage recovery type is to log the Request queue. I do not understand the specific cases clearly. I will try again later when appropriate examples are available.
10. Thinking about the essence of the command mode 10.1:
Encapsulate requests
10.2 when to choose
You need to abstract the actions to be executed and parameterize these objects.
Requests must be arranged and executed at different times.
Cancellation is required
When a system crash is supported, the system operation function can be re-executed.