Command Pattern

The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

Command Pattern Diagram

Implementation

Creating a Command interface:

public interface Command {
	public void execute();
}

Creating a Invoker:

public class RemoteControl {
 
	Command[] onCommands;
	Command[] offCommands;
	
	public RemoteControl() {
		onCommands = new Command[7];
		offCommands = new Command[7];
		
		Command noCommand = new NoCommand();
		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();
	}
 
	public String toString() {
		StringBuffer stringBuff = new StringBuffer();
		stringBuff.append("\n------ Remote Control -------\n");
		
		for (int i = 0; i < onCommands.length; i++) {
			stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
			+ " " + offCommands[i].getClass().getName() + "\n");
		}
 
		return stringBuff.toString();
	}
}

Creating Concrete Commands:

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

Creating a Receiver:

public class Light {
	String location = "";
	public Light(String location) {
		this.location = location;	
	}
	
	/* Actions */
	public void on() {
		System.out.println(location + " light is on");
	}
	public void off() {
		System.out.println(location + " light is off");
	}
}

Testing from Client:

public class RemoteLoader {
 
	public static void main(String[] args) {
	
		/* Invoker */
		RemoteControl remoteControl = new RemoteControl();
	
		/* Recievers */	
		Light livingRoomLight = new Light("Living Room");
		CeilingFan ceilingFan= new CeilingFan("Living Room");
		Stereo stereo = new Stereo("Living Room");
	
		/* Concrete Commands */
		LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
		LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
		CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
		CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
		StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
		StereoOffCommand stereoOff = new StereoOffCommand(stereo);
		
		/* Set which button from Invoker maps to which Commnad */
		remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
		remoteControl.setCommand(1, ceilingFanOn, ceilingFanOff);
		remoteControl.setCommand(2, stereoOnWithCD, stereoOff);
		
		System.out.println(remoteControl);
		remoteControl.onButtonWasPushed(0);
		remoteControl.offButtonWasPushed(0);
		remoteControl.onButtonWasPushed(1);
		remoteControl.offButtonWasPushed(1);
		remoteControl.onButtonWasPushed(2);
		remoteControl.offButtonWasPushed(2);
	}
}