Design Mode-Command pattern undo

Source: Internet
Author: User
Command pattern undo


Address: http://blog.csdn.net/caroline_wendy


Test command mode:Http://blog.csdn.net/caroline_wendy/article/details/31379977


Command modeCan be used for runningUndo operation.


Detailed method:

1. object classes are requiredSave status, Such as level.

package command;public class CeilingFan {String location = "";int level;public static final int HIGH = 3;public static final int MEDIUM = 2;public static final int LOW = 1;public static final int OFF = 0; public CeilingFan(String location) {this.location = location;}  public void high() {// turns the ceiling fan on to highlevel = HIGH;System.out.println(location + " ceiling fan is on high"); } public void medium() {// turns the ceiling fan on to mediumlevel = MEDIUM;System.out.println(location + " ceiling fan is on medium");}public void low() {// turns the ceiling fan on to lowlevel = LOW;System.out.println(location + " ceiling fan is on low");} public void off() {// turns the ceiling fan offlevel = OFF;System.out.println(location + " ceiling fan is off");} public int getSpeed() {return level;}}

2. command interface, including undo (UNDO) operation, that is, the detailed undo action is inferred based on the number of states of the input object.

/*** @ Time June 9, 2014 */package command;/*** @ author C. l. wang **/public interface command {public void execute (); Public void undo (); // undo}

3. Concrete command)Class must be implemented and abolished, based on Different statusesTo run different undo operations.

/*** @ Time June 16, 2014 */package command;/*** @ author C. l. wang **/public class ceilingfanhighcommand implements command {ceilingfan; int prevspeed; Public ceilingfanhighcommand (ceilingfan) {This. ceilingfan = ceilingfan;}/* (non-javadoc) * @ see command. command # execute () */@ overridepublic void execute () {// todo auto-generated method stubprevspeed = ceilingfan. getspeed (); ceilingfan. high ();}/* (non-javadoc) * @ see command. command # undo () */@ overridepublic void undo () {// todo auto-generated method stubif (prevspeed = ceilingfan. high) {ceilingfan. high ();} else if (prevspeed = ceilingfan. medium) {ceilingfan. medium ();} else if (prevspeed = ceilingfan. low) {ceilingfan. low ();} else if (prevspeed = ceilingfan. off) {ceilingfan. off () ;}} package command; public class ceilingfanmediumcommand implements command {ceilingfan; int prevspeed; Public ceilingfanmediumcommand (ceilingfan) {This. ceilingfan = ceilingfan;} public void execute () {prevspeed = ceilingfan. getspeed (); ceilingfan. medium ();} public void undo () {If (prevspeed = ceilingfan. high) {ceilingfan. high ();} else if (prevspeed = ceilingfan. medium) {ceilingfan. medium ();} else if (prevspeed = ceilingfan. low) {ceilingfan. low ();} else if (prevspeed = ceilingfan. off) {ceilingfan. off () ;}} package command; public class ceilingfanlowcommand implements command {ceilingfan; int prevspeed; Public ceilingfanlowcommand (ceilingfan) {This. ceilingfan = ceilingfan;} public void execute () {prevspeed = ceilingfan. getspeed (); ceilingfan. low ();} public void undo () {If (prevspeed = ceilingfan. high) {ceilingfan. high ();} else if (prevspeed = ceilingfan. medium) {ceilingfan. medium ();} else if (prevspeed = ceilingfan. low) {ceilingfan. low ();} else if (prevspeed = ceilingfan. off) {ceilingfan. off () ;}} package command; public class ceilingfanoffcommand implements command {ceilingfan; int prevspeed; Public ceilingfanoffcommand (ceilingfan) {This. ceilingfan = ceilingfan;} public void execute () {prevspeed = ceilingfan. getspeed (); ceilingfan. off ();} public void undo () {If (prevspeed = ceilingfan. high) {ceilingfan. high ();} else if (prevspeed = ceilingfan. medium) {ceilingfan. medium ();} else if (prevspeed = ceilingfan. low) {ceilingfan. low ();} else if (prevspeed = ceilingfan. off) {ceilingfan. off ();}}}

4. Receiver classImplementation Undo)Operation, that is, when a command is called, Reserved command, Run Undo operation, Call Reserved commands.

/*** @ Time June 16, 2014 */package command;/*** @ author C. l. wang **/public class remotecontrol {Command [] oncommands; // enable command [] offcommands; // disable command undocommand; // cancel public remotecontrol () {oncommands = new command [7]; offcommands = new command [7]; command nocommand = new nocommand (); For (INT I = 0; I <7; ++ I) {// initialize oncommands [I] = nocommand; offcommands [I] = nocommand;} undocommand = nocommand;} public void setcommand (INT slot, command oncommand, command offcommand) {This. oncommands [Slot] = oncommand; this. offcommands [Slot] = offcommand;} public void onbuttonwaspushed (INT slot) {// reset cute (); undocommand = oncommands [Slot];} public void offbuttonwaspushed (INT slot) {// close buttonoffcommands[slot0000.exe cute (); undocommand = offcommands [Slot];} public void undobuttonwaspushed () {undocommand. undo ();} Public String tostring () {stringbuffer = new stringbuffer (); stringbuffer. append ("\ n ------ Remote Control ------ \ n"); For (INT I = 0; I <oncommands. length; ++ I) {stringbuffer. append ("[slot" + I + "]" + oncommands [I]. getclass (). getname () + "" + offcommands [I]. getclass (). getname () + "\ n");} return stringbuffer. tostring ();}}


5.Trial Class, Call different commands, save different statuses, and run the Undo operation.

/*** @ Time June 16, 2014 */package command; import javax. crypto. spec. ivparameterspec;/*** @ author C. l. wang **/public class remoteloader {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubremotecontrol remotecontrol = new remotecontrol (); light livingroomlight = new light ("living room"); light kitchenlight = new light ("Kitchen "); ceilingfan = new ceilingfan ("living room"); garagedoor = new garagedoor (""); stereo = new stereo ("living room "); lightoncommand completion = new lightoncommand (livingroomlight); lightoffcommand completion = new lightoffcommand (livingroomlight); lightoncommand kitchenlighton = new lightoncommand (kitchenlight); completion = new completion (kitchenlight ); ceilingfanhighcommand ceilingfanhigh = new ceilingfanhighcommand (ceilingfan); Response response = new response (ceilingfan); Response ceilingfanoff = new response (ceilingfan); Response garagedooron = new response (garagedoor ); export garagedooroff = new external (garagedoor); stereoonwithcdcommand stereoonwithcd = new stereoonwithcdcommand (STEREO); stereooffcommand = new stereooffcommand (STEREO); remotecontrol. setcommand (0, livingroomlighton, livingroomlightoff); // set this remote control remotecontrol. setcommand (1, kitchenlighton, kitchenlightoff); remotecontrol. setcommand (2, ceilingfanhigh, ceilingfanoff); remotecontrol. setcommand (3, ceilingfanmedium, ceilingfanoff); remotecontrol. setcommand (4, stereoonwithcd, stereooffcommand); remotecontrol. onbuttonwaspushed (2); // rapid remotecontrol. offbuttonwaspushed (2); // disable quick system. out. println (remotecontrol); remotecontrol. undobuttonwaspushed (); // return to quick system. out. println (); remotecontrol. onbuttonwaspushed (3); // Medium Speed System. out. println (remotecontrol); remotecontrol. undobuttonwaspushed (); // fast }}


6. Output:

Living Room ceiling fan is on highLiving Room ceiling fan is off------ Remote Control ------[slot 0] command.LightOnCommand    command.LightOffCommand[slot 1] command.LightOnCommand    command.LightOffCommand[slot 2] command.CeilingFanHighCommand    command.CeilingFanOffCommand[slot 3] command.CeilingFanMediumCommand    command.CeilingFanOffCommand[slot 4] command.StereoOnWithCDCommand    command.StereoOffCommand[slot 5] command.NoCommand    command.NoCommand[slot 6] command.NoCommand    command.NoCommandLiving Room ceiling fan is on highLiving Room ceiling fan is on medium------ Remote Control ------[slot 0] command.LightOnCommand    command.LightOffCommand[slot 1] command.LightOnCommand    command.LightOffCommand[slot 2] command.CeilingFanHighCommand    command.CeilingFanOffCommand[slot 3] command.CeilingFanMediumCommand    command.CeilingFanOffCommand[slot 4] command.StereoOnWithCDCommand    command.StereoOffCommand[slot 5] command.NoCommand    command.NoCommand[slot 6] command.NoCommand    command.NoCommandLiving Room ceiling fan is on high


Other code downloads: http://download.csdn.net/detail/u012515223/7507147








Design Mode-Command pattern undo

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.