Command mode of grinding design mode-3

Source: Internet
Author: User
ArticleDirectory
    • 3.3 unrecoverable operations
3.3 unrecoverable operations

The operation that can be undone means to discard the operation and return to the status before the operation is executed.. This function is a very important function, and almost all GUI applications have the function of revoking operations. The GUI menu is one of the most typical applications of the command mode, so you can always find such menu items on the menu.
How can this problem be achieved when it is so common?
There are two basic ideas for Undo operations,One is the compensation type, also known as the Anti-Operation TypeFor example, if the Undo operation is a plus function, the Undo implementation is a minus function. Similarly, the Undo operation is an open function, the Undo function is disabled.
Another method is storage recovery.It means to record the status before the operation, and then restore it directly when you want to cancel the operation.
Here we will talk about the first method, that is, the compensation method or the reverse operation method. The second method will be explained in the memorandum mode. To help you better understand the function of the unrecoverable operation, it is clear to use an example.
1: Sample requirements
Considering the function of a calculator, the simplest one can only implement addition and subtraction operations. Now we need to allow this calculator to support unrecoverable operations.
2: compensatory or Inverse Solution
(1) Before implementing a command interface, first define an interface that truly implements computing. Without such a command, nothing can be done. An example of an operation interfaceCodeAs follows:

Code

  1   /**  
2 * Operation interface
3 */
4 Public Interface Operationapi {
5 /**
6 * Obtain the computing result
7 * @ Return Results After Calculation
8 */
9 Public Int Getresult ();
10 /**
11 * Set the initial value for calculation.
12 * @ Param Result calculates the initial value.
13 */
14 Public Void Setresult ( Int Result );
15 /**
16 * Execute Addition
17 * @ Param Num number to be added
18 */
19 Public Void Add ( Int Num );
20 /**
21 * Perform subtraction.
22 * @ Param Num number to be subtracted
23 */
24 Public Void Substract ( Int Num );
25 }
26

Defines the interface to see how to actually execute addition and subtraction. The sample code is as follows:

 
/*** Operation class: Implements addition/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 ;}}

(2) Next, abstract the command interface. To support the recoverable function, in addition to defining an execution method as before, you also need to define a method for revoking the operation, the sample code is as follows:

 
/*** Command interface: Declares the operation to be executed. You can revoke the operation */public interface command {/*** the operation corresponding to the command to be executed */Public void execute (); /*** the operation corresponding to the execution of the undo command */Public void undo ();}

(3) The commands should be implemented. The specific commands are divided into addition commands and subtraction commands. Let's take a look at the implementation of addition commands. The sample code is as follows:

/*** 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; Public void execute () {// calls 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);}/*** 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 ;}}

The subtraction command is similar to the addition method, but the addition method is reversed during implementation. The sample code is as follows:

/*** 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 );}}

(4) Next we should look at the calculator. The calculator is equivalent to invoker, holding multiple command objects. The calculator is the place where undo operations can be implemented.
To better understand the recoverable functions, Let's first look at what the unrecoverable calculator class looks like, and then add the unrecoverable function example. The sample code is as follows:

/*** Calculator class: Add button and subtract button on the calculator */public class calculator {/*** holding the command object for executing addition */private command addcmd = NULL; /*** command object holding the subtraction operation */private command substractcmd = NULL; /*** set the command object for executing addition * @ Param addcmd the command object for executing addition */Public void setaddcmd (command addcmd) {This. addcmd = addcmd;}/*** set the command object for executing the subtraction operation * @ Param substractcmd command object for executing the subtraction operation */Public void setsubstractcmd (command substractcmd) {This. substractcmd = substractcmd;}/*** is provided to the customer to execute the addition function */Public void addpressed(%%%this.add%.exe cute, execute the subtraction function */Public void substractpressed({%this.substract;.exe cute ();}}

At present, it seems that the above example is almost implemented. Now we can add the function of the Undo operation to this basic implementation.
To implement the Undo operation, you must first record the commands that have been operated to form a Historical List of commands. The Undo operation starts from the last one. Therefore, add the command history list in the calculator class. The sample code is as follows:

 
/*** Command operation history. Use */private list <command> undocmds = new arraylist <command> () to cancel the operation ();

When can I add value to the command history?
The answer is that when each operation button is pressed, that is, when you operate the addition or subtraction button, the sample code is as follows:

 
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 );}

Add the last Undo button in the calculator class. If it is pressed, extract the last command from the command history to undo it, after Undo is complete, you need to delete the undo command from the history, which is equivalent to not executing the command. The sample code is as follows:

 
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 (); // then delete the last command, this. undocmds. remove (CMD);} else {system. out. println ("Sorry, there is no unrecoverable command ");}}

In the same way, you can also implement the restoration function. You can also set a recoverable list for restoration. You can retrieve the last command from the list and execute it again when you need to restore it, the sample code is as follows:

 
/*** Indicates the history of command revocation. Use */private list <command> redocmds = new arraylist <command> () during restoration ();

So when should we assign values to this set?You should note that the recovered command data comes from the undo command. That is to say, only the undo command can be restored. Therefore, assign a value to the set during undo, note that the value must be assigned before the undo command is deleted. The sample code is as follows:

 
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, there is no unrecoverable command ");}}

So how can we achieve recovery? See the sample code:

 
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, there is no recoverable command ");}}

 

Okay. I explained the calculator class step by step. Let's take a look at the complete calculator class code:

/*** Calculator class: add and subtract buttons to the calculator, and record the operation history 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 (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(redocmds.size(-11_1_1_1_.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 ");}}}

(5) It's time to get the harvest. Write a client, assemble the command and receiver, and perform several commands to test the Undo and restore functions, the sample code is as follows:

Public class client {public static void main (string [] ARGs) {// 1: assemble the command and 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 ());}}

(6) run the following command to check the result and enjoy the Undo and undo operations. The result is as follows:

 
The result after an addition operation is: 5 results after a subtraction operation: 2 results after a cancellation: 5 results after a revocation: 0. The result after one recovery operation is: 5. the result after another recovery operation is: 2.

That is, if the initial value is 0, the two commands are executed first with 5 and then 3. It looks easy, right.

 

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.