Download presentation
Presentation is loading. Please wait.
Published byAbner Banks Modified over 9 years ago
1
CS 350 – Software Design Command Object Remote Control Object
2
CS 350 – Software Design Command Object Simple Command Object Implementation public interface Command { public void execute(); public void execute();} public class LightOnCommand implements Command { Light light; Light light; public LightOnCommand(Light light) { public LightOnCommand(Light light) { this.light = light; this.light = light;} public void execute () { public void execute () { light.on(); light.on(); }}
3
CS 350 – Software Design Command Object public class SimpleRemoteControl { Command slot; Command slot; public SimpleRemoteControl { public SimpleRemoteControl { public void setCommand(Command command) { public void setCommand(Command command) { slot = command; slot = command; } public void buttonWasPressed() { public void buttonWasPressed() { slot.execute(); slot.execute(); }}
4
CS 350 – Software Design Command Object public class RemoteControlTest { public static void main(String[] args) { public static void main(String[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); SimpleRemoteControl remote = new SimpleRemoteControl(); Light light = new Light(); Light light = new Light(); LightOnCommand lightOn = new LightOnCommand(light); LightOnCommand lightOn = new LightOnCommand(light); remote.setCommand(lightOn); remote.setCommand(lightOn); remote.buttonWasPressed(); remote.buttonWasPressed(); }}
5
CS 350 – Software Design Command Object SHOW COMMAND PATTERN
6
CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL public class RemoteControl { Command[] on Commands; Command[] on Commands; Command[] offCommands; Command[] offCommands; public RemoteControl() { public RemoteControl() { onCommands = new Command[7]; onCommands = new Command[7]; offCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); Command noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand; }} public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; onCommand[slot] = onCommand; offCommand[slot] = offCommand; offCommand[slot] = offCommand;}
7
CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL public void onButtonWasPushed(int slot) { onCommands(slot].execute(); onCommands(slot].execute();} public void offButtonWasPushed(int slot) { offCommands(slot].execute(); offCommands(slot].execute();} public String toString() { StringBuffer stringBuff = newStringBuffer(); StringBuffer stringBuff = newStringBuffer(); strongBuff.append(“\n------ Remote Control ------ \n”); strongBuff.append(“\n------ Remote Control ------ \n”); for (int i = 0; i < onCommands.length; i++) { for (int i = 0; i < onCommands.length; i++) { stringBuff.append(“[slot “ + i + “] “ + onCommands[i].getClass().getName() + stringBuff.append(“[slot “ + i + “] “ + onCommands[i].getClass().getName() + “ “ + offcommands[i].getClass().getName() + “\n”; “ “ + offcommands[i].getClass().getName() + “\n”; } return stringBuff.toString(); } return stringBuff.toString(); }}
8
CS 350 – Software Design Command Object IMPLEMENTING THE COMMANDS public class LightOffCommand implements Command { Light light; Light light; public LightOffCommand(Light light) { public LightOffCommand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.off(); light.off(); }}
9
CS 350 – Software Design Command Object IMPLEMENTING THE COMMANDS public class StereoOnWithCDCommand implements Command { Stereo stereo; Stereo stereo; public StereoOnWithCDCommand(Stereo stereo) { public StereoOnWithCDCommand(Stereo stereo) { this.stereo = stereo; this.stereo = stereo; public void execute() { public void execute() { stereo.on(); stereo.on(); stereo.setCD(); stereo.setCD(); stereo.setVolume(11); stereo.setVolume(11); }}
10
CS 350 – Software Design Command Object REMOTE CONTROL public class RemoteLoader { public static void main(String[] args) { public static void main(String[] args) { RemoteControl remoteControl = new RemoteControl(); RemoteControl remoteControl = new RemoteControl(); Light livingRoom = newLight(“Living Room”); Light livingRoom = newLight(“Living Room”); Light kitchenLight = new Light(“Kitchen”); Light kitchenLight = new Light(“Kitchen”); CeilingFan ceilingFan = new CeilingFan(“Living Room”); CeilingFan ceilingFan = new CeilingFan(“Living Room”); GarageDoor garageDoor = new GarageDoor(“”); GarageDoor garageDoor = new GarageDoor(“”); Stereo stereo = new Stereo (“Living Room”); Stereo stereo = new Stereo (“Living Room”); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCoammd livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOffCoammd livingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenRoomLight); LightOnCommand kitchenLightOn = new LightOnCommand(kitchenRoomLight); LightOffCommand kitchenLightOn = new LightOffCommand(kitchenRoomLight); LightOffCommand kitchenLightOn = new LightOffCommand(kitchenRoomLight);
11
CS 350 – Software Design Command Object REMOTE CONTROL CeilingFanOnCommand ceilingFanOn = new ceilingFanOnCommand(ceilingFan); CeilingFanOnCommand ceilingFanOn = new ceilingFanOnCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new ceilingFanOffCommand(ceilingFan); CeilingFanOffCommand ceilingFanOff = new ceilingFanOffCommand(ceilingFan); GarageDoorUpCommand garageDoorUp = new garageDoorUpCommand(garageDoor); GarageDoorUpCommand garageDoorUp = new garageDoorUpCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new garageDoorDownCommand(garageDoor); GarageDoorDownCommand garageDoorDown = new garageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); StereoOffCommand stereoOff = new StereoOffCommand(stereo); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); System.out.println(remoteControl); System.out.println(remoteControl);
12
CS 350 – Software Design Command Object REMOTE CONTROL remoteControl.onButtonWasPushed(0); remoteControl.onButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.onButtonWasPushed(1); remoteControl.onButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.onButtonWasPushed(2); remoteControl.onButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.onButtonWasPushed(3); remoteControl.onButtonWasPushed(3); remoteControl.offButtonWasPushed(3); remoteControl.offButtonWasPushed(3);
13
CS 350 – Software Design Command Object REMOTE CONTROL – SHORT CUTS We do not want to code as follows: public void onButtonWasPushed(int slot) { if (onCommands[slot[ != null) { if (onCommands[slot[ != null) { onCommands[slot].execute(); onCommands[slot].execute(); }} Instead we code: public class NoCommand implements Command ( public void execute() { } public void execute() { }} In the remote control constructor we code: Command noCommand = new NoCommand(): for (int i = 0; i < 7; i++) onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand;}
14
CS 350 – Software Design Command Object ADDING UNDO public interface Command { public void execute(); public void execute(); public void undo(); public void undo();} Easy for some objects public class LightOnCommand implements Command { Light light; Light light; public LightOnCOmmand(Light light) { public LightOnCOmmand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.on(); light.on(); } public void undo() { public void undo() { light.off(); light.off(); }}
15
CS 350 – Software Design Command Object ADDING UNDO public class LightOffCommand implements Command { Light light; Light light; public LightOffCommand(Light light) { public LightOffCommand(Light light) { this.light = light; this.light = light; } public void execute() { public void execute() { light.off(); light.off(); } public void undo() { public void undo() { light.on(); light.on(); }}
16
CS 350 – Software Design Command Object IMPLEMENTING THE REMOTE CONTROL WITH UNDO public class RemoteControl { Command[] on Commands; Command[] on Commands; Command[] offCommands; Command[] offCommands; Command[] undoCommands; Command[] undoCommands; public RemoteControl() { public RemoteControl() { onCommands = new Command[7]; onCommands = new Command[7]; offCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); Command noCommand = new NoCommand(); for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) { onCommands[i] = noCommand; onCommands[i] = noCommand; offCommands[i] = noCommand; offCommands[i] = noCommand; } undoCommand = noCommand; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; onCommand[slot] = onCommand; offCommand[slot] = offCommand; offCommand[slot] = offCommand;}
17
CS 350 – Software Design Command Object public void onButtonWasPushed(int slot) { onCommands(slot].execute(); onCommands(slot].execute(); undoCommand = onCommands[slot]; undoCommand = onCommands[slot];} public void offButtonWasPushed(int slot) { offCommands(slot].execute(); offCommands(slot].execute(); undoCommand = offCommands[slot]; undoCommand = offCommands[slot];} public void undoButtonwWasPushed() { undoCommand.undo(); undoCommand.undo();} public String toString() { //same}
18
CS 350 – Software Design Command Object public class CeilingFan { public static final int HIGH = 3; public static final int HIGH = 3; public static final int MEDIUM = 2; public static final int MEDIUM = 2; public static final int LOW= 1; public static final int LOW= 1; public static final int OFF= 0; public static final int OFF= 0; public CeilingFan(String location) { public CeilingFan(String location) { this.location = location; this.location = location; speed = OFF; speed = OFF; } public void high() { public void high() { speed = HIGH; speed = HIGH; //code to set fan to high //code to set fan to high} public void medium() { public void medium() { speed = MEDIUM; speed = MEDIUM; //code to set fan to medium //code to set fan to medium}
19
CS 350 – Software Design Command Object public void low() { public void low() { speed = LOW; speed = LOW; //code to set fan to low //code to set fan to low} public void off() { public void off() { speed = OFF; speed = OFF; //code to turn fan off //code to turn fan off} public int getSpeed() { return speed; return speed; }}
20
CS 350 – Software Design Command Object Adding Undo to the ceiling fan commands public class CeilingFanHighCommand implements Command { CeilingFan ceilingFan; CeilingFan ceilingFan; int prevSpeed; int prevSpeed; public CeilingFanHighCommand(CeilingFan ceilingFan) { public CeilingFanHighCommand(CeilingFan ceilingFan) { this.ceilingFan = ceilingFan; this.ceilingFan = ceilingFan;} public void execute() { public void execute() { prevSpeed = ceilingFan.getSpeed(); prevSpeed = ceilingFan.getSpeed(); ceilingFan.high(); ceilingFan.high();} public void undo() { if (prevSpeed == CeilingFan.HIGH) { if (prevSpeed == CeilingFan.HIGH) { celingFan.high(); celingFan.high(); } else if (prevSpeed = CeilingFan.MEDIUM) { } else if (prevSpeed = CeilingFan.MEDIUM) { ceilingFan.medium(); ceilingFan.medium(); } else if (prevSpeed = CeilingFan.LOW) { } else if (prevSpeed = CeilingFan.LOW) { ceilingFan.low(); ceilingFan.low(); } else if (prevSpeed = CeilingFan.OFF) { } else if (prevSpeed = CeilingFan.OFF) { ceilingFan.off();}} ceilingFan.off();}}
21
CS 350 – Software Design Command Object MACRO COMMAND public class MacroCommand implements Command { Command[] commands; Command[] commands; public MacroCommand(Command[] commands) { public MacroCommand(Command[] commands) { this.commands = commands; this.commands = commands;} public void execute() { public void execute() { for (int i = 0; i < commands.length; i++) { for (int i = 0; i < commands.length; i++) { commands[i].execute(); commands[i].execute(); } }}
22
CS 350 – Software Design Command Object USING A MACRO COMMAND Light light = new Light(“Living Room”); TV tv = new TV(“Living Room”); Stereo stereo = new Stereo(“Living Room”); Hottub hottub = new Hottub(); LightOnCommand lightOn = new LightOnCommand(light); StereoOnCommand stereoOn = new StereoOnCommand(stereo); TVOnCommand tvOn = newTVOnCommand(tv); HotTubOnCommand hottubOn = newHottubOnCommand(hottub); LightOffCommand lightOff = new LightOffCommand(light); StereoOffCommand stereoOff = new StereoOffCommand(stereo); TVOffCommand tvOff = newTVOffCommand(tv); HotTubOffCommand hottubOff = newHottubOffCommand(hottub); Command[] partyOn = (lightOn, stereoOn, tvOn, hottubOn); Command[] partyOff = (lightOff, stereoOff, tvOff, hottubOff); MacroCommand partyOnMacro = newMacroCommand(partyOn); MacroCommand partyOffMacro = newMacroCommand(partyOff); Remotecontrol.setCommand(0, partyOnMacro, partyOffMacro);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.