Upload
abner-banks
View
218
Download
0
Embed Size (px)
Citation preview
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
Remote Control ObjectRemote Control Object
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
Simple Command Object ImplementationSimple Command Object Implementation
public interface Command {public interface Command { public void execute();public void execute();}}
public class LightOnCommand implements Command {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
public class SimpleRemoteControl {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
public class RemoteControlTest {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
SHOW COMMAND PATTERNSHOW COMMAND PATTERN
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
IMPLEMENTING THE REMOTE CONTROLIMPLEMENTING THE REMOTE CONTROL
public class RemoteControl {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 public void setCommand(int slot, Command onCommand, Command offCommand) {offCommand) {
onCommand[slot] = onCommand;onCommand[slot] = onCommand; offCommand[slot] = offCommand;offCommand[slot] = offCommand;}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
IMPLEMENTING THE REMOTE CONTROLIMPLEMENTING THE REMOTE CONTROL
public void onButtonWasPushed(int slot) {public void onButtonWasPushed(int slot) { onCommands(slot].execute();onCommands(slot].execute();}}
public void offButtonWasPushed(int slot) {public void offButtonWasPushed(int slot) { offCommands(slot].execute();offCommands(slot].execute();}}
public String toString() {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();
}}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
IMPLEMENTING THE COMMANDSIMPLEMENTING THE COMMANDS
public class LightOffCommand implements Command {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
IMPLEMENTING THE COMMANDSIMPLEMENTING THE COMMANDS
public class StereoOnWithCDCommand implements Command {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); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
REMOTE CONTROLREMOTE CONTROL
public class RemoteLoader {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 livingRoomLightOn = new LightOnCommand(livingRoomLight);LightOnCommand(livingRoomLight);
LightOffCoammd livingRoomLightOff = new LightOffCoammd livingRoomLightOff = new LightOffCommand(livingRoomLight);LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn = new LightOnCommand kitchenLightOn = new LightOnCommand(kitchenRoomLight);LightOnCommand(kitchenRoomLight);
LightOffCommand kitchenLightOn = new LightOffCommand kitchenLightOn = new LightOffCommand(kitchenRoomLight);LightOffCommand(kitchenRoomLight);
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
REMOTE CONTROLREMOTE CONTROL
CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand ceilingFanOn = new ceilingFanOnCommand(ceilingFan);ceilingFanOnCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand ceilingFanOff = new ceilingFanOffCommand(ceilingFan);ceilingFanOffCommand(ceilingFan);
GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand garageDoorUp = new garageDoorUpCommand(garageDoor);garageDoorUpCommand(garageDoor);
GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand garageDoorDown = new garageDoorDownCommand(garageDoor);garageDoorDownCommand(garageDoor);
StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);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);
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
REMOTE CONTROLREMOTE 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);
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
REMOTE CONTROL – SHORT CUTSREMOTE CONTROL – SHORT CUTS
We do not want to code as follows:We do not want to code as follows:
public void onButtonWasPushed(int slot) {public void onButtonWasPushed(int slot) { if (onCommands[slot[ != null) {if (onCommands[slot[ != null) { onCommands[slot].execute();onCommands[slot].execute(); }}}}
Instead we code:Instead we code:
public class NoCommand implements Command (public class NoCommand implements Command ( public void execute() { }public void execute() { }}}
In the remote control constructor we code:In the remote control constructor we code:
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;}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
ADDING UNDOADDING UNDO
public interface Command {public interface Command { public void execute();public void execute(); public void undo();public void undo();}}
Easy for some objectsEasy for some objects
public class LightOnCommand implements Command {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
ADDING UNDOADDING UNDO
public class LightOffCommand implements Command {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(); }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
IMPLEMENTING THE REMOTE CONTROL WITH UNDOIMPLEMENTING THE REMOTE CONTROL WITH UNDO
public class RemoteControl {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;undoCommand = noCommand;}}
public void setCommand(int slot, Command onCommand, Command public void setCommand(int slot, Command onCommand, Command offCommand) {offCommand) {
onCommand[slot] = onCommand;onCommand[slot] = onCommand; offCommand[slot] = offCommand;offCommand[slot] = offCommand;}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
public void onButtonWasPushed(int slot) {public void onButtonWasPushed(int slot) { onCommands(slot].execute();onCommands(slot].execute(); undoCommand = onCommands[slot];undoCommand = onCommands[slot];
}}
public void offButtonWasPushed(int slot) {public void offButtonWasPushed(int slot) { offCommands(slot].execute();offCommands(slot].execute(); undoCommand = offCommands[slot];undoCommand = offCommands[slot];
}}
public void undoButtonwWasPushed() {public void undoButtonwWasPushed() { undoCommand.undo();undoCommand.undo();}}
public String toString() {public String toString() {//same//same}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
public class CeilingFan {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}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand 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() {public int getSpeed() { return speed;return speed; }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
Adding Undo to the ceiling fan commandsAdding Undo to the ceiling fan commands
public class CeilingFanHighCommand implements Command {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() {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();}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
MACRO COMMANDMACRO COMMAND
public class MacroCommand implements 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(); }} }}}}
CS 350 – Software DesignCS 350 – Software DesignCommand ObjectCommand Object
USING A MACRO COMMANDUSING A MACRO COMMAND
Light light = new Light(“Living Room”);Light light = new Light(“Living Room”);TV tv = new TV(“Living Room”);TV tv = new TV(“Living Room”);Stereo stereo = new Stereo(“Living Room”);Stereo stereo = new Stereo(“Living Room”);Hottub hottub = new Hottub();Hottub hottub = new Hottub();
LightOnCommand lightOn = new LightOnCommand(light);LightOnCommand lightOn = new LightOnCommand(light);StereoOnCommand stereoOn = new StereoOnCommand(stereo);StereoOnCommand stereoOn = new StereoOnCommand(stereo);TVOnCommand tvOn = newTVOnCommand(tv);TVOnCommand tvOn = newTVOnCommand(tv);HotTubOnCommand hottubOn = newHottubOnCommand(hottub);HotTubOnCommand hottubOn = newHottubOnCommand(hottub);
LightOffCommand lightOff = new LightOffCommand(light);LightOffCommand lightOff = new LightOffCommand(light);StereoOffCommand stereoOff = new StereoOffCommand(stereo);StereoOffCommand stereoOff = new StereoOffCommand(stereo);TVOffCommand tvOff = newTVOffCommand(tv);TVOffCommand tvOff = newTVOffCommand(tv);HotTubOffCommand hottubOff = newHottubOffCommand(hottub);HotTubOffCommand hottubOff = newHottubOffCommand(hottub);
Command[] partyOn = (lightOn, stereoOn, tvOn, hottubOn);Command[] partyOn = (lightOn, stereoOn, tvOn, hottubOn);Command[] partyOff = (lightOff, stereoOff, tvOff, hottubOff);Command[] partyOff = (lightOff, stereoOff, tvOff, hottubOff);
MacroCommand partyOnMacro = newMacroCommand(partyOn);MacroCommand partyOnMacro = newMacroCommand(partyOn);MacroCommand partyOffMacro = newMacroCommand(partyOff);MacroCommand partyOffMacro = newMacroCommand(partyOff);
Remotecontrol.setCommand(0, partyOnMacro, partyOffMacro);Remotecontrol.setCommand(0, partyOnMacro, partyOffMacro);