Design Mode: manually write a command mode
. Command mode: encapsulate the "request" as an object to parameterize other objects using different requests, queues, and logs. The command mode also supports unrecoverable operations.
The command object includes the action and the receiver (as the executor) into the object. The exposed execute () method of this object. commands can also be used to implement logs and transaction systems. Macro commands are a simple extension of commands, allowing you to call multiple commands.
The following is a class diagram of the command mode:
The source code of a case is as follows:
Package command;/*** Command object interface * @ author Arvon **/public interface command {public void execute (); public void undo ();}
package command;public class LightOnCommand implements Command {private Light light;public LightOnCommand(Light light) {super();this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.on();}@Overridepublic void undo() {// TODO Auto-generated method stublight.off();}}
package command;public class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {super();this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.off();}@Overridepublic void undo() {// TODO Auto-generated method stublight.on();}}
package command;public class CeillingFanHighCommand implements Command {private CeilingFan fan;private int prevSpeed;@Overridepublic void execute() {prevSpeed = fan.getSpeed();fan.high();}public CeillingFanHighCommand(CeilingFan fan) {super();this.fan = fan;}@Overridepublic void undo() {if (prevSpeed == CeilingFan.HIGH) {fan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {fan.medium();} else if (prevSpeed == CeilingFan.LOW) {fan.low();} else if (prevSpeed == CeilingFan.OFF) {fan.off();}}}
package command;public class CeillingFanOffCommand implements Command {CeilingFan ceilingFan;int prevSpeed; public CeillingFanOffCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}@Overridepublic void execute() {// TODO Auto-generated method stubprevSpeed = ceilingFan.getSpeed();ceilingFan.off();}@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;/*** do not empty objects at all * @ author Administrator **/public class NoCommand implements Command {@ Overridepublic void execute () {// TODO Auto-generated method stub} @ Overridepublic void undo () {// TODO Auto-generated method stub }}
Package command;/*** Remote Control Invoker manages many command objects * @ author Arvon **/public class RemoteControl {Command [] onCommands; Command [] offCommands; /*** record the previous Command */Command undoCommand;/*** Initialize all commands */public RemoteControl () {super (); onCommands = new Command [5]; offCommands = new Command [5]; for (int I = 0; I <5; I ++) {Command noCommand = new NoCommand (); onCommands [I] = noCommand; offCommands [I] = noCommand;}/*** set Set the Command * @ param slot number of slots * @ param onCommand * @ param offCommand */public void setCommand (int slot, Command onCommand, Command offCommand) {onCommands [slot] = onCommand; offCommands [slot] = offCommand;} public void onButtonWasPush (int route cute (); undoCommand = onCommands [slot];} public void offButtonWasPush (int slot){offcommands[slot;.exe cute (); undoCommand = offCommands [slot];} public Void undoButtonWasPush () {if (undoCommand! = Null) undoCommand. undo ();} public String toString () {StringBuffer stringBuff = new StringBuffer (); stringBuff. append (------ Remote Control -------); for (int I = 0; I <onCommands. length; I ++) {stringBuff. append ([slot + I +] + onCommands [I]. getClass (). getSimpleName () ++ offCommands [I]. getClass (). getSimpleName () +);} stringBuff. append ([undo] + undoCommand. getClass (). getSimpleName () +); return stringBuff. toString ();}}
Package command;/*** lamp command executor * @ author Administrator **/public class Light {public void on () {// TODO Auto-generated method stubSystem. out. println (light is on ...);} public void off () {// TODO Auto-generated method stubSystem. out. println (light is off ...);}}
Package command;/*** executor of the fan command * @ author Administrator **/public class CeilingFan {public static final int HIGH = 3; public static final int MEDIUM = 2; public static final int LOW = 1; public static final int OFF = 0; String location; int speed; public CeilingFan (String location) {this. location = location; speed = OFF;} public void high () {speed = HIGH; System. out. println (location + ceiling fan is on high);} public void medium () {speed = MEDIUM; System. out. println (location + ceiling fan is on medium);} public void low () {speed = LOW; System. out. println (location + ceiling fan is on low);} public void off () {speed = OFF; System. out. println (location + ceiling fan is off);} public int getSpeed () {return speed ;}}
Package command;/*** test class ** @ author Administrator **/public class CommandTest {public static void main (String [] args) {// TODO Auto-generated method stubRemoteControl control = new RemoteControl (); Light light = new Light (); Command lightOn = new LightOnCommand (light ); command lightOff = new LightOffCommand (light); control. setCommand (0, lightOn, lightOff); control. onButtonWasPush (0); control. offButtonWasPush (0); control. undoButtonWasPush (); CeilingFan fan = new CeilingFan (living room); Command execution = new CeillingFanHighCommand (fan); Command CeillingFanOffCommand = new CeillingFanOffCommand (fan); control. setCommand (1, mCeillingFanHighCommand, CeillingFanOffCommand); control. onButtonWasPush (1); control. offButtonWasPush (1); control. onButtonWasPush (1); control. undoButtonWasPush ();}}
Program output:
Light is on...
Light is off...
Light is on...
Living room ceiling fan is on high
Living room ceiling fan is off
Living room ceiling fan is on high
Living room ceiling fan is off