In this article we will learn Command Design Pattern by Designing an Event Handling Framework.

Intent of Command Design Pattern

Encapsulate a request as an object, thereby letting you parametrize clients with different requests, queue or log requests, and support undo able operations.[GoF]

What a heavy Intent 🙂

Lets learn step by step,

Command Design Pattern revolves around Command. It’s main responsibility is to separate out the invoker from the receiver of commands.
To Do this it converts the Command into a Abstract Command Object and maintain a map of REQUEST_KEY and COMMAND Objects. Receiver Defines the Concrete Command Objects and registers them with specific Request Key in this map.

When Invoker receives a Request it fetches the associated Command Object from map and just execute that. Thus Invoker is not aware of Which Command Object is actually receiving that Object.

Command Design Pattern

Also Concrete Command Object which is actually executing that Request is unaware of the fact that who Invoked him.

Now lets see an example. Suppose We are,

Designing a framework which has a capability to listen the USB and Wifi Events from OS

It has an Invoker which reads different Events from OS like,

  • USB has been Plugged
  • USB has been UnPlugged
  • Wifi is Connected
  • Wifi is Disconnected

Invoker knows how to listen for these events from OS and identify them. But it doesn’t know what to do after receiving it because that is application dependent.

So, what Invoker will do after receiving event ?

Invoker has a map of Events and Command Object. For every received Event it will fetch a Command object and call execute on that. But it will not be aware of what’s actually happening inside this Command Object.

Command is actually a Abstract class.

The map inside Invoker contains the Concrete Command Objects inside Command Pointer.

So, Invoker is not concerned or aware of what’s actually happening on calling Command Object’s execute. Its main concern is listening the Events and invoking corresponding Command Object. Invoker exposes the interfaces to register Command Objects corresponding to events.

Application that is using our framework contains the Receiver.


In this module, all operations are defined that need to be called on execution of different commands. These operations are defined as Concrete Command Classes representing different Command Objects.

Concrete Command Classes

Let’s see the concrete Command Classes,

This module creates the Concrete Command Objects and register these Command Objects with Invoker i.e.

In real environment these Events will be fired from inside the framework when actual hardware action takes place.
But here we will simulate by invoking them manually.
At the time of invocation Invoker don’t know which command is going to execute. Also Receiver of Command i.e. the Concrete Command Object doesn’t how has invoked him.

Just like execute() operation we can add undo() operation in Command class hierarchy to achieve the undo capability.

Complete executable code is as follows. To execute this code use following code,

g++ –std=c++11 command.cpp

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.