Command Pattern in Java

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

The definition is a bit confusing at first but let's step through it. In analogy to our problem above remote control is the client and stereo, lights etc. are the receivers. In command pattern there is a command object that encapsulates a request by binding together a set of actions on a specific receiver. It does so by exposing just one method execute() that causes some actions to be invoked on the receiver.

Parameterizing other objects with different requests in our analogy means that the button used to turn on the lights can later be used to turn on stereo or may be open the garage door.

queue or log requests, and support undoable operations means that Command's Execute operation can store state for reversing its effects in the Command itself. The Command may have an added un Execute operation that reverses the effects of a previous call to execute. It may also support logging changes so that they can be reapplied in case of a system crash.

RemoteControlTest.java

Output:

Light is on
Stereo is on
Stereo is set for CD input
Stereo volume set to 11
Stereo is off

Command Pattern Important Points

  • Command is the core of command design pattern that defines the contract for implementation.
  • Receiver implementation is separate from command implementation.
  • Command implementation classes chose the method to invoke on receiver object, for every method in receiver there will be a command implementation. It works as a bridge between receiver and action methods.
  • Invoker class just forward the request from client to the command object.
  • Client is responsible to instantiate appropriate command and receiver implementation and then associate them together.
  • Client is also responsible for instantiating invoker object and associating command object with it and execute the action method.
  • Command design pattern is easily extendible, we can add new action methods in receivers and create new Command implementations without changing the client code.
  • The drawback with Command design pattern is that the code gets huge and confusing with high number of action methods and because of so many associations.

Benefits of Command Pattern

  • Decoupling: The command pattern decouples the client from the receiver. It makes the code more flexible and reusable.
  • Undo/redo: The command pattern can be used to implement undo/redo functionality. It can be useful for applications such as text editors and image editors.
  • Queuing: The command pattern can be used to implement queuing functionality. It can be useful for applications such as job schedulers and message queues.

The command pattern is a powerful and versatile design pattern that can be used to solve a variety of problems. It is a good choice for applications where we need to decouple the client from the receiver, implement undo/redo functionality, or implement queuing functionality.






Latest Courses