Skip to content

Behavioral Design Pattern: Command Pattern

Command Pattern is a Behavioral Design Pattern. Command Pattern encapsulates any request to perform the task into a specific object. These objects later can be used to store them in Queue or Stack as needed by the application. It also enhance the flexibility as client will not bother which command is performed and how they are performed. A single command can be configured to do multiple things that are related to each other.

Motivation and Intent

Lets see the command pattern in a little depth. Command pattern defines a process by which client code can interact with the system by triggering commands. These commands actually encapsulates what they will do when passed to the system or their behavior. The client will never bother about how and what the commands will actually do. The client will just create a few commands and trigger them to run on the system. This result in decoupling of the client which invokes the command and actual work the command perform. Ultimately it will enhance the flexibility of the source code. Any update in the invocation logic will not result in an update in the command execution. Same is true if a execution logic of a command changes it will have no effect on the invoking logic. A command will act as a black box to the Client.


Command pattern is pretty simple to implement. You just need to create an interface or abstract class that will act as behavior provider. This behavior will then be extended or realize by other classes which will act as commands in our system. Whenever client want to create and execute a task then the client just need to create a command and execute it. Well this all sounds quite simple. Now look at the class diagram.

Command Pattern UML diagram
Command Pattern UML diagram

Lets see how it is actually implemented. We’ll start with our interface. Let’s call it ICommand.

Now we need some concrete command classes that will implement the above behavior. In this example we have two concrete classes.

Next we need a client that will create and execute the commands. This is also pretty straight forward. So without chit-chatting a lot lets see the code.

In the above example, we can see that our client code does not know anything about how actually the command is executed. It just execute the command.

Practical Example

Above example told us quite a lot about the command pattern. Let’s see a more practical example. In this example we will try to mimic the banking system. In any banking system we want a completely atomic transaction. This means either the complete transaction should be a success or none of it. First we will see the class diagram and then try to code that class diagram.

Command Pattern UML diagram
Command Pattern UML diagram

From the above class diagram we have an ICommand like interface ITransaction. But this interface has two methods one execute() and rollback(). The execute() method will execute the command and rollback() method will rollback the commands execution.

There are different kinds of transaction any banking system will do such as ShareData, Authenticate, Debit, Acknowledgement etc. So we need to define concrete classes for all of them. All of these classes will implement the ITransaction interface.

In our last example, our client code was the one that was creating the command and executing it. But we can decouple the execution logic as well. In this example, lets assume we have an executioner class called TransactionExecutioner. This class will be responsible to execute all the commands client passes to it. This class will also make sure the Atomic nature of the complete transaction steps. If any of the step failed everything should be rolled back in the reverse order they were executed. Here’s the logic.

Now we have our Commands and our execution system in place. The only thing left now is the client code. The client code is pretty easy to understand. So just go through the code.

We create an executioner object give it some commands to work with and ask it to execute everything and forget about it. Client code does not consider anything that will happen when commands are being executed. Only the end result matters.

Well this is it for the command pattern. Hope this was of some help.

As always thank you for reading and comments are always welcome.


Published inDesign Patterns

Be First to Comment

    Leave a Reply

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