jobs4timesLogo jobs4timesLogo

Command Design Pattern


  • Command design pattern is the one Behavioral Design pattern from Gang Of Four Design Patterns.
  • It is used to encapsulate a request as an Object and pass to an invoker.
  • Invoker doesn't know how to service the request but uses encapsulated command object to perform the action.

Typically in a command design pattern there are five actors involved there are as follows :
  1. Command : It is an interface with execute method. It acts as a contract.
  2. Client : Client instantiates an concrete command object and associates it with a receiver.
  3. Invoker : He instructs the command to perform an action.
  4. Concrete Command : Associates a binding between receiver and action.
  5. Receiver : It is the object that knows the actual steps to perform the action.

  • Let's consider an example to understand it. For example PowerOn and PowerOff are the commands, to turn on/off the Television. These commands are received by the Television.
  • You will issue these commands using the remote controller who acts as an Invoker. Client is the person who uses this remote Control.

  • The advantage of this is invoker is decoupled by the action performed by the receiver.
  • The Invoker has no knowledge of the receiver.The Invoker issues a command where in the command performs the action on a receiver.
  • The Invoker doesn't know the details of the action being performed. So, changes to the receiver action don't affect the invoker's action.
Here is the code snippet explaining the same, The interface acts as a core contract for commands.
package design;

public interface Command {
     public void execute();	
}

Implementing this interface we have 2 commands, PowerOn and PowerOff .

package design;

  //command, encapsulated with receiver to perform action
public class PowerOn implements Command{
	
	 //receiver on who command performs the action 
	private Television television;
	
	public PowerOn(Television television) {
		this.television=television;
	}

  @Override
  public void execute() {
    television.on();
  }

}

package design;

  //command, encapsulated with receiver to perform action
public class PowerOff implements Command{
	
	 //receiver on who command performs the action 
	private Television television;
	
	public PowerOff(Television television) {
		this.television=television;
	}

  @Override
  public void execute() {
    television.off();
  }

}

In the above Television is the receiver on who the command is issuing the action.

package design;

 //receiver (he knows how to perform the action)
public class Television {

  public void on() {
	System.out.println("Television switcher on ...");
  }
  
  public void off() {
	System.out.println("Television turning off ...");
  }

}

Now remote control is the invoker who can issue several commands and command triggers an action on the receiver who knows how to to handle that action.

package design;

public class RemoteControl {
	
	private Command command;
	
	public RemoteControl(Command command){
		this.command=command;
	}
	
	public void pressButton(){
		command.execute();
	}

}

(OR)

package design;

public class RemoteControl {
	
	private Command command;
	
	public RemoteControl(Command command){
		this.command=command;
	}
	
	public RemoteControl() { }

	public void pressButton(){
		command.execute();
	}

	public void setCommand(PowerOn onCmd) {
		command=onCmd;		
	}

	public void setCommand(PowerOff offCmd) {
		command=offCmd;		
	}

}

Finally Client is the Person who issues a command on the Invoker.

package design;

public class Person {
	
 public static void main(String[] args){
	 
	 //Invoker
	RemoteControl control=new RemoteControl();
	
	//receiver
	Television television=new Television();
	
	
	//command setup with receiver
	PowerOn onCmd=new PowerOn(television);
	control.setCommand(onCmd);
	control.pressButton();
	
	PowerOff offCmd=new PowerOff(television);
	control.setCommand(offCmd);
	control.pressButton();
 }
	
}
output :
Television switcher on ...
Television turning off ...
The UML representation of Command design pattern :
command-design-pattern

BACK