Command Design Pattern – Implementation in Java

I have discussed Singleton PatternObserver PatternFactory Pattern and Decorator Pattern in my previous posts. In this one I will be talking about Command Design Pattern.

What is a Command Pattern?

Command Design Pattern is a pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters. This helps you to have loose coupling between the invoker and the receiver.

The invoker does not need to know the receiver. And both can be updated individually, as long as the contract is maintained.

Advantages of Command Design Pattern

  1. It allows you to convert a method call into individual object giving you the flexibility to use it in parameters.
  2. It allows you to queue operations, schedule invocations or reverse operations..
  3. It embodies the single responsibility design principle, since one command class is responsible for a single action.

Disadvantages of Command Design Pattern

  1. The application size can increase a lot as you can have many small command classes.

Terminologies

  1. Command Interface – for declaring an operation
  2. Concrete Command classes – Extends the command interface and invokes the business logic on the receiver.
  3. Invoker – Which calls the operation.
  4. Receiver – Executes the operation.

UML diagram

In the above diagram, we have the Device as the interface. Television is the receiver class. TurnTVOn and TurnTVOff are the command classes. Invoker class is not present in the UML diagram. It would be the class from which the method execute is called (e.g. Remote Class)

Java Implementation

Let us take an example. We want to write a function for a Remote, such that each button takes in the device it is supposed to operate on. We use the command pattern for this. For the sake of keeping this example simple, we have only two functions being supported by the remote, on and off. And we only have the TV to operate on.

First let us define the device interface and the device implementations. In this example that would be the Television class.

public interface Device {
public void on();
public void off();
}
view raw Device.java hosted with ❤ by GitHub
public class Television implements Device {
@Override
public void on() {
System.out.println("TV is on !!");
}
@Override
public void off() {
System.out.println("TV is off !!");
}
}
view raw Television.java hosted with ❤ by GitHub

Next we define the Command interface.

public interface Command {
public void execute();
}
view raw Command.java hosted with ❤ by GitHub

Next let us implement the two commands TurnTVOn and TurnTVOff

public class TurnTVOn implements Command {
Device device;
public TurnTVOn(Device device) {
this.device = device;
}
@Override
public void execute() {
device.on();
}
}
public class TurnTVOff implements Command {
Device device;
public TurnTVOff(Device device) {
this.device = device;
}
@Override
public void execute() {
device.off();
}
}

Let us define the Remote class. The Remote class does not need to be aware of which device it is working on.

public class Remote {
Device device;
public Remote(Device device) {
this.device = device;
}
}
view raw Remote.java hosted with ❤ by GitHub

Bringing it all together in Main.

public static void main(String[] args) {
Device device = new Television();
Remote remote = new Remote(device);
// Turn TV on
Command cmd = new TurnTVOn(device);
cmd.execute();
//Turn TV Off
cmd = new TurnTVOff(device);
cmd.execute();
}
view raw Main.java hosted with ❤ by GitHub

You can find the complete code on Github.

Conclusion:

The Command pattern implementation allows you to keep a stack of commands and help undo/redo the actions executed. Its a pattern that is widely used in applications that have a GUI, for example, your text editor apps.

Reference

http://www.newthinktank.com/2012/09/command-design-pattern-tutorial/

One thought on “Command Design Pattern – Implementation in Java

Comments are closed.