Understanding and Implementing the Command Pattern in C++

The idea of this article is to understand what is Command Pattern, when is it useful, and how can we get a basic implementation of Command Pattern using C++.


There are many a times in our applications that we want to perform multiple operations on the same data. I mean scenarios when the user has some data and he has an option of doing one out of many operations on that data. An example could be an image processor: I could choose to rotate, flip and/or invert colors of the photo. Also I could find myself not happy with the action and I may want to undo my action. Now this is all business logic that I have to place in my code. Now what I could do is have my action invoker call the methods of the action receiver/taker and achieve this. But then I will have to write a lot of conditional code. I will have to write very similar code for each action. Also, If I have more commands, I will find myself changing the existing code very often.

Instead of taking the above approach, I could devise a mechanism where all my actions can somehow be handled polymorphically thus giving me a clean and less error prone code/design. This idea is the basic intent of writing a Command Pattern.

The Command Pattern talks about encapsulating all the requests coming from the invoker in objects, passing them to the receiver, and letting the receiver take actions. If I have many actions to perform, I can have a hierarchy of commands and handle them polymorphically.

GoF defines Command Pattern as “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.” So let us look at the class diagram for the Command pattern and try to understand it.


  • Command: This is an interface for executing an action.
  • ConcreteCommand: This object specifies the binding between a Receiver/action taker and an action invoker. This object is responsible for executing the corresponding operation on the Receiver.
  • Client: Creates a ConcreteCommand object and sets its receiver.
  • Invoker: It will use the Command object to carry out the request.
  • Receiver: It contains the real operational logic that needs to be performed on the data.

Using the code

Let us now see how to have a simple Command pattern implementation by writing a small Calculator sort of application. We will focus on understanding the Command pattern so we will operate on a fixed set of data and will try to perform basic operations on them. Let’s say we will be creating an application like this:


Note: The idea here is to understand the Command Pattern and not write a calculator. Also, this example is small so Command Pattern might seem an overkill for this.

So let us start by writing the Command class.

This class is an abstract class and will be used as an interface for the execution of commands. Let us now write the ConcreteCommand classes that will inherit this abstract class.

We have the command hierarchy for our application ready. Now let us work on writing the real application logic. We will need a class that will do the calculations, i.e., the Reciever. Let us have an interface to represent the major functionalities of the receiver.

and the concrete implementation of the Reciever, i.e., our Calculator class

We will also be needing a class that will let the user choose the command to execute, i.e., the Client (this will be the UI in our case). Finally, we will write an Invoker which will again be the user interface in our case.

The event change of radio buttons are simply changing the command object to be used, and when the user chooses to get the result, the appropriate command object will be used to fetch the result for him.

Points of interest

Command Pattern is a very good way of decreasing the coupling between the sender and receiver. The most important thing to remember while implementing the Command pattern is that the Command is just a link between the sender and receiver. It should only tell the receiver what the sender is expecting. It should never alter the logic of the sender and receiver in any way. I have a similar implementation of Command Pattern in C#. Please refer to that if you want to see the C# code and the subtle similarities and differences in code.

Download sample code for this article: CommandTestCpp