Decoupling request senders from receivers-command mode (4)

Source: Internet
Author: User
5. Implementation of the Undo operation

In command mode, we can call the execute () method of a command object to process the request. If you need to cancel the (UNDO) request, you can add a reverse operation to the command class.

Extension

In additionReverse operationIn addition to undo, you can also useSave the Historical Status of an objectThe latter can be implemented using the memento pattern.

The following uses a simple example to learn how to use the command mode to perform the Undo operation:

Sunny software company wants to develop a simple calculator, which can realize simple mathematical operations and cancel operations on the operations.

Developers of Sunny software company designed the 5-chart structure using the command mode. The calculator Interface Class calculatorform acts as the request sender, And the adder class that implements the data summation function acts as the request receiver, the interface class can indirectly call the add () method in the addition class to implement addition operations, and provides the Undo () method that can cancel addition operations.

Figure 5 simple calculator Structure

The complete code of this instance is as follows:

// Addition class: the requested receiver class adder {private int num = 0; // defines the initial value as 0 // addition operation. The incoming value and num are added each time, return the result to public int add (INT value) {num + = value; return num ;}// abstract command class abstract class abstractcommand {public abstract int execute (INT value ); // declare the command execution method execute () public abstract int undo (); // declare the revocation method undo ()} // The specific command class concretecommand extends abstractcommand {private adder = new adder (); Private int value; // implements the execute () method declared in the abstract command class, call the addition operation public int execute (INT value) {This. value = value; return adder. add (value) ;}// implement the Undo () method declared in the abstract command class. Add an inverse number to implement the inverse public int undo () {return adder. add (-value) ;}// calculator Interface Class: Request sender class calculatorform {private abstractcommand command; Public void setcommand (abstractcommand command) {This. command = command;} // call the execute () method of the command object to execute the public void compute (INT value) {int I = command.exe cute (value); system. out. println ("run the operation. The operation result is:" + I);} // call the Undo () method of the command object to undo public void undo () {int I = command. undo (); system. out. println ("execution undo, Calculation Result:" + I );}}

Write the following client test code:

Class client {public static void main (string ARGs []) {calculatorform form = new calculatorform (); abstractcommand command; command = new concretecommand (); form. setcommand (command); // inject the command object form to the sender. compute (10); form. compute (5); form. compute (10); form. undo ();}}

Compile and run the program. The output result is as follows:

Run the operation. The operation result is 10.

Run the operation. The operation result is: 15.

Run the operation. The operation result is: 25.

Cancel the operation. The calculation result is: 15.

 

Thoughts

If "form. Undo ()" is called twice consecutively, the output result of the client code is predicted.

Note that only one Undo operation can be performed in this instance, because the Historical Status of the command object is not saved, you can import a command set or other methods to store the command status for each operation, so as to implement multiple Undo operations. In addition to the Undo operation, you can also use a similar method to perform a recovery (redo) operation, that is, to restore the Undo operation (or calledSecondary Revocation).

Exercise

Modify the source code of the simple calculator to enable multiple Undo and redo operations ).

 

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.