COMMAND PATTERN

The command pattern is a behavioral object design pattern. In the command pattern, a Command interface declares a method for executing a particular action. Concrete Command classes implement the execute () method of the Command interface, and this execute () method invokes the appropriate action method of a Receiver class that the Concrete Command class contains. The Receiver class performs a particular action. A Client class is responsible for creating a Concrete Command and setting the Receiver of the Concrete Command. An Invoker class contains a reference to a Command and has a method to execute the Command.

In the command pattern, the invoker is decoupled from the action performed by the receiver. The invoker has no knowledge of the receiver. The invoker invokes a command, and the command executes the appropriate action of the receiver. Thus, the invoker can invoke commands without knowing the details of the action to be performed. In addition, this decoupling means that changes to the receiver’s action don’t directly affect the invocation of the action.

The command pattern can be used to perform ‘undo’ functionality. In this case, the Command interface should include an unexecuted () method.

In other words, it’s used to implement lose coupling in a request-response model. In command pattern, the request is send to the invoker and invoker passes it to the encapsulated command object. Command object passes the request to the appropriate method of Receiver to perform the specific action. The client program creates the receiver object and then attaches it to the Command. Then it creates the invoker object and attaches the command object to perform an action. Now when client program executes the action, it’s processed based on the command and receiver object.

To understand command design pattern we should understand the associated key terms like client, command, command implementation, invoker, and receiver.

  • Command is an interface with execute method. It is the core of contract.
  • A client creates an instance of a command implementation and associates it with a receiver.
  • An invoker instructs the command to perform an action.
  • A Command implementation’s instance creates a binding between the receiver and an action.
  • Receiver is the object that knows the actual steps to perform the action. Any class may serve as a Receiver.

Example:

package com.myjavablog.behavioural.command;

//Command interface with an execute method
public interface Command {
public void execute();
}

package com.myjavablog.behavioural.command;

//Lunch is a receiver
public class Lunch {

public void makeLunch(){
System.out.println("Lunch is being made");
}
}

package com.myjavablog.behavioural.command;

//LunchCommand implements Command. It contains reference to Lunch ,a receviver.
//Its execute method invokes appropriate action on receiver
public class LunchCommand implements Command{

Lunch lunch;

public LunchCommand(Lunch lunch){
this.lunch = lunch;
}

@Override
public void execute() {
lunch.makeLunch();
}
}

package com.myjavablog.behavioural.command;

//Dinner is a receiver
public class Dinner {

public void makeDinner()
{
System.out.println("Dinner is being made");
}
}

package com.myjavablog.behavioural.command;

//DinnerCommand also implements Command. It contains reference to Dinner ,a receviver.
//Its execute method invokes appropriate action on receiver
public class DinnerCommand implements Command{

Dinner dinner;

public DinnerCommand(Dinner dinner){
this.dinner = dinner;
}

@Override
public void execute()
{
dinner.makeDinner();
}
}

package com.myjavablog.behavioural.command;

//MealInvoker is invoker class
//It contains reference to command to invoke
//Its invoke method calls execute method of command
public class MealInvoker {

Command command;

public MealInvoker(Command command){
this.command = command;
}

public void setCommand(Command command){
this.command = command;
}

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

}

package com.myjavablog.behavioural.command;

public class CommandPatternDemo {

public static void main(String[] args) {

Lunch lunch = new Lunch(); //Receiver
Command lunchCommand = new LunchCommand(lunch); //Concrete command

Dinner dinner = new Dinner(); //Receiver
Command dinnerCommand = new DinnerCommand(dinner); //Concrete command

MealInvoker mealInvoker = new MealInvoker(lunchCommand); //invoker
mealInvoker.invoke();

mealInvoker.setCommand(dinnerCommand);
mealInvoker.invoke();

}
}

Output:

Lunch is being made
Dinner is being made

 

When to use the Command Design Pattern:

Use the Command pattern when you want to:

  • Parameterize objects by an action to perform.
  • Support undo. The Command’s Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Un-execute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling Un-execute and Execute, respectively.
  • Structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the
    system with new transactions.

Command Pattern JDK Example:

  • Runnable Interface (java.lang.Runnable) and Swing Action (javax.swing.Action) uses command pattern.

Leave a Comment

Bitnami