I am here to continue the discussion around Design Patterns. Today we will go through one of the behavioral design pattern called Command.
Also in case you have not had a look at our previous articles, go through the following link:
- Design Patterns Simplified: Part 1
- Design Patterns Simplified: Singleton - Part 2
- Design Patterns Simplified: Simple Factory - Part 3
- Design Patterns Simplified: Abstract Factory - Part 4
- Design Patterns Simplified: Factory Method - Part 5
- Design Patterns Simplified: Prototype - Part 6
- Design Patterns Simplified: Builder - Part 7
- Design Patterns Simplified: Facade - Part 8
- Design Patterns Simplified: Adapter - Part 9
- Design Patterns Simplified: Decorator- Part 10
- Design Patterns Simplified: Bridge - Part 11
- Design Patterns Simplified: Composite - Part 12
- Design Patterns Simplified: Proxy - Part 13
As per GOF guys, Command pattern is defined as in the following.
“Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”
Well! Let’s understand what they mean and where this pattern can be fit into.
It means that command pattern wraps the requests into objects and hence provides better control over how to manage various user actions.
This can be used in different Windows based applications (Text Editor, Image Editor andCalculator etc.) where we want better control over user actions or commands. In WPF, DelegateCommand, RelayCommand and RoutedCommands are the example of Command pattern.
It is more suited in cases when we want to implement Undo, Redo, and Copy/Paste kind of operations by storing command or action as an object.
If we were to achieve similar control and flexibility without using command pattern, we need to write lot of repeatedcode in various user actions and maintenance of such code becomes difficult for any future change.
Now let’s see the UML of Command pattern.
Command pattern has five key components as following.
- Command: This is an interface that holds the generic methods related to actions.
- Receiver: This class keep the methods for real operations.
- Concreate Command: This class implements the Command interface and invokes the receiver’s operation.
- Invoker: Asks command object to execute the action.
- Client: First creates Concreate Command objects bysetting the receiver then calls the Invoker by setting the concreate command objects.
How Command pattern works?
Invoker calls the Execute methods by passing requests wrapped in command objects. Based on the passed command object, respective concreate command implementation is selected and that in turn invokes the receiver method to do the actual work.
Let's understand this further by simple example.
Let’s begin by creating the Command interface as following.
- ///<summary>
- /// The Command interface
- ///</summary>
- public interface ICommand
- {
- bool CanEexecute
- {
- get;
- }
- string Name
- {
- get;
- }
- void Execute();
- }


Prakash TripathiPosted May 27, 2016, 5:17 PM
Thnx Vignesh.
Vignesh ManiPosted May 27, 2016, 4:26 PM
nice