Command Pattern in Java
Command Pattern Diagram
The following example illustrates the callback mechanism achieved by means of the Command pattern.
The example shows a Fan and a Light. Our objective is to develop a Switch that can turn either object on or off.
We see that the Fan and the Light have different interfaces, which means the Switch has to be independent of the Receiver interface or it has no knowledge of the Receiver's interface. To solve this problem, we need to parameterize each of the Switchs with the appropriate command. The Switch connected to the Light will have a different command than the Switch connected to the Fan.
The Command class has to be
- declared as abstract or
- an interface
for this to work.
When the constructor for a Switch is invoked, it is parameterized with the appropriate set of commands. The commands will be stored as private variables of the Switch. When the
flipUp()
and
flipDown()
operations are called, they will simply make the appropriate command to execute( ). The Switch will not be aware as a result of the
execute()
method being called.
package com.java.behavioral.command;
class Fan {
public void startRotate() {
System.out.println("Fan is rotating");
}
public void stopRotate() {
System.out.println("Fan is not rotating");
}
}
class Light {
public void turnOn( ) {
System.out.println("Light is on ");
}
public void turnOff( ) {
System.out.println("Light is off");
}
}
class Switch {
private Command UpCommand, DownCommand;
public Switch( Command Up, Command Down) {
// concrete Command registers itself with the invoker
UpCommand = Up;
DownCommand = Down;
}
/* invoker calls back concrete Command,
which executes the Command on the receiver */
void flipUp( ) {
UpCommand . execute ( ) ;
}
void flipDown( ) {
DownCommand . execute ( );
}
}
class LightOnCommand implements Command {
private Light myLight;
public LightOnCommand ( Light L) {
myLight = L;
}
public void execute( ) {
myLight . turnOn( );
}
}
class LightOffCommand implements Command {
private Light myLight;
public LightOffCommand ( Light L) {
myLight = L;
}
public void execute( ) {
myLight . turnOff( );
}
}
class FanOnCommand implements Command {
private Fan myFan;
public FanOnCommand ( Fan F) {
myFan = F;
}
public void execute( ) {
myFan . startRotate( );
}
}
class FanOffCommand implements Command {
private Fan myFan;
public FanOffCommand ( Fan F) {
myFan = F;
}
public void execute( ) {
myFan . stopRotate( );
}
}
package com.java.behavioral.command;
public class TestCommand {
public static void main(String[] args) {
Light testLight = new Light( );
LightOnCommand testLOC = new LightOnCommand(testLight);
LightOffCommand testLFC = new LightOffCommand(testLight);
Switch testSwitch = new Switch( testLOC,testLFC);
testSwitch.flipUp( );
testSwitch.flipDown( );
Fan testFan = new Fan( );
FanOnCommand foc = new FanOnCommand(testFan);
FanOffCommand ffc = new FanOffCommand(testFan);
Switch ts = new Switch( foc,ffc);
ts.flipUp( );
ts.flipDown( );
}
}
package com.java.behavioral.command;
public interface Command {
public abstract void execute ( );
}
Program output when run from TestCommand.java
Light is on
Light is off
Fan is rotating
Fan is not rotating