Studyon Minte9.com
Head First (Patterns)




Command Pattern



* Light on/off

	%java

		// Remote slot doesn't care what object has, 
		// as long as it implements the Command interface

		package testapptmp;

		public class Testapptmp { // Client
		    
		    private static String device;
		    private static String command;
			
		    public static void main(String[] args) {        
			SimpleRemoteControl remote = new SimpleRemoteControl(); // Invoker

			//device = "light"; command = "off";
			device = "garageDoor"; command = "down";
			
			if (device.equals("light")) {
			    
			    Light light = new Light(); // Command
			    
			    if (command.equals("on")) {
				LightOnCommand lightOn = new LightOnCommand(light); // Receiver
				remote.setCommand(lightOn);
			    }
			    
			    if (command.equals("off")) {
				LightOffCommand lightOff = new LightOffCommand(light);
				remote.setCommand(lightOff);
			    }
			}
			
			if (device.equals("garageDoor")) {
			    
			    GarageDoor garageDoor = new GarageDoor();
			    
			    if (command.equals("up")) {
				GarageDoorUpCommand GarageDoorUp = 
                                           new GarageDoorUpCommand(garageDoor);
				remote.setCommand(GarageDoorUp);
			    }
			    
			    if (command.equals("down")) {
				GarageDoorDownCommand GarageDoorDown = 
                                           new GarageDoorDownCommand(garageDoor);
				remote.setCommand(GarageDoorDown);
			    }
			}
			
			remote.buttonWasPressed();
                                 // garageDoor is down
		 
		    }
		}

		interface Command {
		    public void execute();
		}

		class SimpleRemoteControl {
		    Command slot;
		    
		    public SimpleRemoteControl() {}
		    
		    public void setCommand(Command command) {
			this.slot = command;
		    }
		    
		    public void buttonWasPressed() {
			slot.execute();
		    }
		}

		// ----------------------------

		class LightOnCommand implements Command {
		    Light light;
		    
		    public LightOnCommand(Light light) {
			this.light = light;
		    }
		    
		    @Override
		    public void execute() {
			light.on();
		    }
		}

		class LightOffCommand implements Command {
		    Light light;
		    
		    public LightOffCommand(Light light) {
			this.light = light;
		    }
		    
		    @Override
		    public void execute() {
			light.off();
		    }
		}

		class Light {
		    public void on() {
			System.out.println("Light is on");
		    }
		    public void off() {
			System.out.println("Light is off");
		    }
		}

		// ----------------------------

		class GarageDoorUpCommand implements Command {
		    GarageDoor garageDoorCommand;
		    
		    public GarageDoorUpCommand(GarageDoor gd) {
			this.garageDoorCommand = gd;
		    }
		    
		    @Override
		    public void execute() {
			garageDoorCommand.up();
		    }
		}

		class GarageDoorDownCommand implements Command {
		    GarageDoor garageDoorCommand;
		    
		    public GarageDoorDownCommand(GarageDoor gd) {
			this.garageDoorCommand = gd;
		    }
		    
		    @Override
		    public void execute() {
			garageDoorCommand.down();
		    }
		}

		class GarageDoor {
		    public void up() {
			System.out.println("GarageDoor is up");
		    }
		    public void down() {
			System.out.println("GarageDoor is down");
		    }
		}



* On/Off arrays

	%java

		package testapptmp;

		public class Testapptmp {
		    
		    public static void main(String[] args) {        
			RemoteControl remoteControl = new RemoteControl();

			Light light = new Light();
			GarageDoor garageDoor = new GarageDoor();

			/* --- Look Here ---- */

			remoteControl.setCommand(0, 
				new LightOnCommand(light), 
				new LightOffCommand(light));
			remoteControl.setCommand(1, 
				new GarageDoorUpCommand(garageDoor), 
				new GarageDoorDownCommand(garageDoor));
			
			remoteControl.onButtonWasPushed(0);
			remoteControl.onButtonWasPushed(1);
			    // Light is on
			    // GarageDoor is up
			
			remoteControl.onButtonWasPushed(2);
			    // Nothing
		    }
		}

		interface Command {
		    public void execute();
		}

		class RemoteControl {
		    
		    Command[] onCommands;
		    Command[] offCommands;
		    
		    public RemoteControl() {
			onCommands = new Command[7];
			offCommands = new Command[7];

			/* --- Look Here ---- */
			
			Command noCommand = new NoCommand();
				// we didn't want to check to see if a command is loaded 
				// every time we referenced a slot 

			for(int i=0; i<7; i++) {
			    onCommands[i] = noCommand;
			    offCommands[i] = noCommand;
			}
		    }
		    
		    public void setCommand(int slot, Command onCommand, Command offCommand) {
			onCommands[slot] = onCommand;
			offCommands[slot] = offCommand;
		    }
		    
		    public void onButtonWasPushed(int slot) {
			onCommands[slot].execute();
		    }
		    
		    public void offButtonWasPushed(int slot) {
			offCommands[slot].execute();
		    }
		}

		// ----------------------------

		class LightOnCommand implements Command {
		    Light light;
		    
		    public LightOnCommand(Light light) {
			this.light = light;
		    }
		    
		    @Override
		    public void execute() {
			light.on();
		    }
		}

		class LightOffCommand implements Command {
		    Light light;
		    
		    public LightOffCommand(Light light) {
			this.light = light;
		    }
		    
		    @Override
		    public void execute() {
			light.off();
		    }
		}

		class Light {
		    public void on() {
			System.out.println("Light is on");
		    }
		    public void off() {
			System.out.println("Light is off");
		    }
		}

		// ----------------------------

		class GarageDoorUpCommand implements Command {
		    GarageDoor garageDoorCommand;
		    
		    public GarageDoorUpCommand(GarageDoor gd) {
			this.garageDoorCommand = gd;
		    }
		    
		    @Override
		    public void execute() {
			garageDoorCommand.up();
		    }
		}

		class GarageDoorDownCommand implements Command {
		    GarageDoor garageDoorCommand;
		    
		    public GarageDoorDownCommand(GarageDoor gd) {
			this.garageDoorCommand = gd;
		    }
		    
		    @Override
		    public void execute() {
			garageDoorCommand.down();
		    }
		}

		class GarageDoor {
		    public void up() {
			System.out.println("GarageDoor is up");
		    }
		    
		    public void down() {
			System.out.println("GarageDoor is down");
		    }
		}

		// ----------------------------
		
		/**
			This is an example of a null object. 
			A null object is usefull when you don't have a meaninfull object to return.
		*/
		class NoCommand implements Command {

		    @Override
		    public void execute() {}
		}