1 / 22

CS 350 – Software Design Command Object

CS 350 – Software Design Command Object. Remote Control Object. CS 350 – Software Design Command Object. Simple Command Object Implementation public interface Command { public void execute(); } public class LightOnCommand implements Command { Light light ;

ivan
Download Presentation

CS 350 – Software Design Command Object

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 350 – Software DesignCommand Object Remote Control Object

  2. CS 350 – Software DesignCommand Object Simple Command Object Implementation public interface Command { public void execute(); } public class LightOnCommand implements Command { Light light; public LightOnCommand(Light light) { this.light = light; } public void execute () { light.on(); } }

  3. CS 350 – Software DesignCommand Object public class SimpleRemoteControl { Command slot; public SimpleRemoteControl { public void setCommand(Command command) { slot = command; } public void buttonWasPressed() { slot.execute(); } }

  4. CS 350 – Software DesignCommand Object public class RemoteControlTest { public static void main(String[] args) { SimpleRemoteControl remote = new SimpleRemoteControl(); Light light = new Light(); LightOnCommandlightOn = new LightOnCommand(light); remote.setCommand(lightOn); remote.buttonWasPressed(); } }

  5. CS 350 – Software DesignCommand Object SHOW COMMAND PATTERN

  6. CS 350 – Software DesignCommand Object IMPLEMENTING THE REMOTE CONTROL public class RemoteControl { Command[] on Commands; Command[] offCommands; public RemoteControl() { onCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); for (inti = 0; i < 7; i++) { onCommands[i] = noCommand; offCommands[i] = noCommand; } } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; offCommand[slot] = offCommand; }

  7. CS 350 – Software DesignCommand Object IMPLEMENTING THE REMOTE CONTROL public void onButtonWasPushed(int slot) { onCommands(slot].execute(); } public void offButtonWasPushed(int slot) { offCommands(slot].execute(); } public String toString() { StringBufferstringBuff = newStringBuffer(); strongBuff.append(“\n------ Remote Control ------ \n”); for (inti = 0; i < onCommands.length; i++) { stringBuff.append(“[slot “ + i + “] “ + onCommands[i].getClass().getName() + “ “ + offcommands[i].getClass().getName() + “\n”; } return stringBuff.toString();} }

  8. CS 350 – Software DesignCommand Object IMPLEMENTING THE COMMANDS public class LightOffCommand implements Command { Light light; public LightOffCommand(Light light) { this.light = light; } public void execute() { light.off(); } }

  9. CS 350 – Software DesignCommand Object IMPLEMENTING THE COMMANDS public class StereoOnWithCDCommand implements Command { Stereo stereo; public StereoOnWithCDCommand(Stereo stereo) { this.stereo = stereo; public void execute() { stereo.on(); stereo.setCD(); stereo.setVolume(11); } }

  10. CS 350 – Software DesignCommand Object REMOTE CONTROL public class RemoteLoader { public static void main(String[] args) { RemoteControlremoteControl = new RemoteControl(); Light livingRoom = newLight(“Living Room”); Light kitchenLight = new Light(“Kitchen”); CeilingFanceilingFan = new CeilingFan(“Living Room”); GarageDoorgarageDoor = new GarageDoor(“”); Stereo stereo = new Stereo (“Living Room”); LightOnCommandlivingRoomLightOn = new LightOnCommand(livingRoomLight); LightOffCoammdlivingRoomLightOff = new LightOffCommand(livingRoomLight); LightOnCommandkitchenLightOn = new LightOnCommand(kitchenRoomLight); LightOffCommandkitchenLightOn = new LightOffCommand(kitchenRoomLight);

  11. CS 350 – Software DesignCommand Object REMOTE CONTROL CeilingFanOnCommandceilingFanOn = new ceilingFanOnCommand(ceilingFan); CeilingFanOffCommandceilingFanOff = new ceilingFanOffCommand(ceilingFan); GarageDoorUpCommandgarageDoorUp = new garageDoorUpCommand(garageDoor); GarageDoorDownCommandgarageDoorDown = new garageDoorDownCommand(garageDoor); StereoOnWithCDCommandstereoOnWithCD = new StereoOnWithCDCommand(stereo); StereoOffCommandstereoOff = new StereoOffCommand(stereo); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); remoteControl.setCommand(3, stereoOnWithCD, stereoOff); System.out.println(remoteControl);

  12. CS 350 – Software DesignCommand Object REMOTE CONTROL remoteControl.onButtonWasPushed(0); remoteControl.offButtonWasPushed(0); remoteControl.onButtonWasPushed(1); remoteControl.offButtonWasPushed(1); remoteControl.onButtonWasPushed(2); remoteControl.offButtonWasPushed(2); remoteControl.onButtonWasPushed(3); remoteControl.offButtonWasPushed(3);

  13. CS 350 – Software DesignCommand Object REMOTE CONTROL – SHORT CUTS We do not want to code as follows: public void onButtonWasPushed(int slot) { if (onCommands[slot[ != null) { onCommands[slot].execute(); } } Instead we code: public class NoCommand implements Command ( public void execute() { } } In the remote control constructor we code: Command noCommand = new NoCommand(): for (inti = 0; i < 7; i++) onCommands[i] = noCommand; offCommands[i] = noCommand; }

  14. CS 350 – Software DesignCommand Object ADDING UNDO public interface Command { public void execute(); public void undo(); } Easy for some objects public class LightOnCommand implements Command { Light light; public LightOnCOmmand(Light light) { this.light = light; } public void execute() { light.on(); } public void undo() { light.off(); } }

  15. CS 350 – Software DesignCommand Object ADDING UNDO public class LightOffCommand implements Command { Light light; public LightOffCommand(Light light) { this.light = light; } public void execute() { light.off(); } public void undo() { light.on(); } }

  16. CS 350 – Software DesignCommand Object IMPLEMENTING THE REMOTE CONTROL WITH UNDO public class RemoteControl { Command[] on Commands; Command[] offCommands; Command[] undoCommands; public RemoteControl() { onCommands = new Command[7]; offCommands = new Command[7]; Command noCommand = new NoCommand(); for (inti = 0; i < 7; i++) { onCommands[i] = noCommand; offCommands[i] = noCommand; } undoCommand = noCommand; } public void setCommand(int slot, Command onCommand, Command offCommand) { onCommand[slot] = onCommand; offCommand[slot] = offCommand; }

  17. CS 350 – Software DesignCommand Object public void onButtonWasPushed(int slot) { onCommands(slot].execute(); undoCommand = onCommands[slot]; } public void offButtonWasPushed(int slot) { offCommands(slot].execute(); undoCommand = offCommands[slot]; } public void undoButtonwWasPushed() { undoCommand.undo(); } public String toString() { //same }

  18. CS 350 – Software DesignCommand Object 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; public CeilingFan(String location) { this.location = location; speed = OFF; } public void high() { speed = HIGH; //code to set fan to high } public void medium() { speed = MEDIUM; //code to set fan to medium }

  19. CS 350 – Software DesignCommand Object public void low() { speed = LOW; //code to set fan to low } public void off() { speed = OFF; //code to turn fan off } public intgetSpeed() { return speed; } }

  20. CS 350 – Software DesignCommand Object Adding Undo to the ceiling fan commands public class CeilingFanHighCommand implements Command { CeilingFanceilingFan; intprevSpeed; public CeilingFanHighCommand(CeilingFanceilingFan) { this.ceilingFan = ceilingFan; } public void execute() { prevSpeed = ceilingFan.getSpeed(); ceilingFan.high(); } public void undo() { if (prevSpeed == CeilingFan.HIGH) { celingFan.high(); } else if (prevSpeed = CeilingFan.MEDIUM) { ceilingFan.medium(); } else if (prevSpeed = CeilingFan.LOW) { ceilingFan.low(); } else if (prevSpeed = CeilingFan.OFF) { ceilingFan.off();}}

  21. CS 350 – Software DesignCommand Object MACRO COMMAND public class MacroCommand implements Command { Command[] commands; public MacroCommand(Command[] commands) { this.commands = commands; } public void execute() { for (inti = 0; i < commands.length; i++) { commands[i].execute(); } } }

  22. CS 350 – Software DesignCommand Object USING A MACRO COMMAND Light light = new Light(“Living Room”); TV tv = new TV(“Living Room”); Stereo stereo = new Stereo(“Living Room”); Hottubhottub = new Hottub(); LightOnCommandlightOn = new LightOnCommand(light); StereoOnCommandstereoOn = new StereoOnCommand(stereo); TVOnCommandtvOn = newTVOnCommand(tv); HotTubOnCommandhottubOn = newHottubOnCommand(hottub); LightOffCommandlightOff = new LightOffCommand(light); StereoOffCommandstereoOff = new StereoOffCommand(stereo); TVOffCommandtvOff = newTVOffCommand(tv); HotTubOffCommandhottubOff = newHottubOffCommand(hottub); Command[] partyOn = (lightOn, stereoOn, tvOn, hottubOn); Command[] partyOff = (lightOff, stereoOff, tvOff, hottubOff); MacroCommandpartyOnMacro = newMacroCommand(partyOn); MacroCommandpartyOffMacro = newMacroCommand(partyOff); Remotecontrol.setCommand(0, partyOnMacro, partyOffMacro);

More Related