The Command pattern is known as a behavioral pattern. Command design pattern is used to implement loose coupling in a request-response model.

The definition of Command provided in the original Gang of Four book on Design Patterns states:

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations

In command pattern, the request is sent to the Invoker and invoker pass 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, create the receiver object and then attach it to the Command. Then it creates the invoker object and attach the command object to perform an action.

Now when client program executes the action, it’s processed based on the command and receiver object.

Command Design Pattern Flow
Command Design Pattern Flow

Let’s understand with Example

One example of the command pattern being executed in the real world is the idea of a table order at a restaurant: the waiter takes the order, which is a command from the customer. This order is then queued for the kitchen staff.

The waiter tells the chef that a new order has come in, and the chef has enough information to cook the meal.

Command Design Pattern in Java
Command Design Pattern


We will look at a table order at a restaurant where we can implement the Command Pattern.

To implement table order, first of all we need to create Receiver class that will actually do all the work.

Since our code in terms of interface in Java, we can have Order interface and it’s implementation classes for different orders such as Lunch, Dinner etc.

Step 1: Receiver Classes of Command Pattern

Receiver class is ready, Let’s implement Command classes.

Step 2: Interface and Implementations of Command Pattern

Use interface or abstract class to create our base Command.

Create interface because it do not have any default implementations.

Now we need to create implementations for all the different types of action performed by the receiver. Since we have three actions we will create two Command implementations. Each Command implementation will forward the request to the appropriate method of Receiver.

Now we have receiver and command implementations ready, so we can move to implement the invoker class.

Step 3: Invoker Class of Command Pattern

Invoker is a simple class that encapsulates the Command and passes the request to the command object to process it.

Our order service is ready now. We need customer to order.

Customer is responsible to provide order as per requirement. The client program is also responsible to attach the receiver to the command and then command to the invoker class.

Output :
Lunch is in progress!!! Dinner is in progress!!!
Advantages of Command Pattern
  1. A command decouples the object that invokes the operation from the one that knows how to perform it.
  2. Commands can be assembled into a composite command.
  3. A command is a first-class object. It can be manipulated and extended like any other object.
  4. It’s easy to add new commands, because we don’t have to change the existing classes.
Drawbacks of Command Pattern
  1. The code gets huge and confusing with high number of action methods and because of so many associations.
  2. If we need to add a new command type we need to change the invoker as well; this would violate the Open Close Principle (OCP).

Leave a Reply

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