Command Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Yet another interesting article in our design patterns implementations series. This article will deal with command design pattern implementation. It allows us to encapsulate commands in objects enabling us to issue requests without knowing requested operations.

Command Design Pattern

Implementation:

Gang of Four defines command pattern as below

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

This article will only deal with encapsulation and parameterization part of command pattern. Consider class diagram.

Command Design Pattern Class Diagram

Command Design Pattern Class Diagram

Notice important components in it.

  • Command: Interface which declares execution method for operation.
  • ConcreteCommand: Class which implements Command interface and provides actual implementation of command execution.
  • Invoker: References command and asks to execute request.
  • Receiver: Object which actually knows how to perform operations asked by commands.
  • Client: Instantiates Receiver, Invoker and concrete commands.

Points to remember:

  • Define Command interface with method execute();
  • Create concrete classes implementing Command and refer Receiver, implement execute();
  • Refer to commands from Invoker.
  • Implement command behaviours in Receiver.
  • Instantiate Invoker, Receiver and concrete commands from Client and call Invoker methods.

Command Design Pattern Implementation Example:

Consider we have a workshop in which customers come to repair their bikes. Workshop manager takes requests on what needs to be done to bike. Those requests are sent in the form of concrete commands to Mechanic. Suppose customer asks the manager to change oil and check electrical system. Our entities become

  • Customer which is a Client
  • Manager which acts as Invoker,
  • ChangeOilCommand and CheckElectricalsCommand are actual commands which implement Command interface.
  • Mechanic acts as Receiver.

Let us get our hands dirty with code:

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout command

If you don’t have git installed, you can either directly download zip or copy paste from below.

Command.java

ChangeOilCommand.java

CheckElectricalsCommand.java

Mechanic.java

Manager.java

CommandType.java

Customer.java

Run this program and you will see output

Here is what happens,

  • Customer instantiates Manager, Mechanic, ChangeOilCommand and CheckElectricalsCommand. Commands are passed reference of Mechanic which acts as receiver and knows how to perform certain things. It also populates the manager with all commands. Those are stored in commands map.
  • Manager calls processRequest() method which, based on type of request, calls methods from Mechanic class. For example, if the request is to change oil, then based on CommandType, ChangeOilCommand is retrieved and execute() method from ChangeOilCommand is called, which in turn calls changeOil() method from Mechanic class because we have implemented it that way.

Thus, command design pattern implementation separates actual commands from their behaviours. Detailed class diagram for our example as below

Command Design Pattern Example Class Diagram

Command Design Pattern Example Class Diagram

I hope this article helped understand Command Design Pattern Implementation in java. Happy learning 🙂

Share Button

1 comment for “Command Design Pattern Implementation

  1. Pankaj
    September 14, 2016 at 4:57 pm

    nice artical prasad……..keep it up 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *