Command design pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE etc. In this article, I would like share what is command pattern and how is it work?
In this pattern, a request is wrapped under an object as a command and passed to invoker object. Invoker object pass the command to the appropriate object which can handle it and that object executes the command. This handles the request in traditional ways like as queuing and callbacks.
This pattern is commonly used in the menu systems of many applications such as Editor, IDE etc.
The UML class diagram for the implementation of the command design pattern is given below:
The classes, interfaces, and objects in the above UML class diagram are as follows:
This is the class that creates and executes the command object.
Asks the command to carry out the action.
This is an interface which specifies the Execute operation.
This is a class that implements the Execute operation by invoking operation(s) on the Receiver.
This is a class that performs the Action associated with the request.
public class Client { public void RunCommand() { Invoker invoker = new Invoker(); Receiver receiver = new Receiver(); ConcreteCommand command = new ConcreteCommand(receiver); command.Parameter = "Dot Net Tricks !!"; invoker.Command = command; invoker.ExecuteCommand(); } } public class Receiver { public void Action(string message) { Console.WriteLine("Action called with message: {0}", message); } } public class Invoker { public ICommand Command { get; set; } public void ExecuteCommand() { Command.Execute(); } } public interface ICommand { void Execute(); } public class ConcreteCommand : ICommand { protected Receiver _receiver; public string Parameter { get; set; } public ConcreteCommand(Receiver receiver) { _receiver = receiver; } public void Execute() { _receiver.Action(Parameter); } }
The classes, interfaces, and objects in the above class diagram can be identified as follows:
Switch- Invoker class.
ICommand - Command interface.
FlipUpCommand and FlipDownCommand - Concrete Command classes.
Light - Receiver class.
/// <summary> /// The 'Command' interface /// </summary> public interface ICommand { void Execute(); } /// <summary> /// The 'Invoker' class /// </summary> public class Switch { private List<ICommand> _commands = new List<ICommand>(); public void StoreAndExecute(ICommand command) { _commands.Add(command); command.Execute(); } } /// <summary> /// The 'Receiver' class /// </summary> public class Light { public void TurnOn() { Console.WriteLine("The light is on"); } public void TurnOff() { Console.WriteLine("The light is off"); } } /// <summary> /// The Command for turning on the light - ConcreteCommand #1 /// </summary> public class FlipUpCommand : ICommand { private Light _light; public FlipUpCommand(Light light) { _light = light; } public void Execute() { _light.TurnOn(); } } /// <summary> /// The Command for turning off the light - ConcreteCommand #2 /// </summary> public class FlipDownCommand : ICommand { private Light _light; public FlipDownCommand(Light light) { _light = light; } public void Execute() { _light.TurnOff(); } } /// <summary> /// Command Pattern Demo /// </summary> class Program { static void Main(string[] args) { Console.WriteLine("Enter Commands (ON/OFF) : "); string cmd = Console.ReadLine(); Light lamp = new Light(); ICommand switchUp = new FlipUpCommand(lamp); ICommand switchDown = new FlipDownCommand(lamp); Switch s = new Switch(); if (cmd == "ON") { s.StoreAndExecute(switchUp); } else if (cmd == "OFF") { s.StoreAndExecute(switchDown); } else { Console.WriteLine("Command \"ON\" or \"OFF\" is required."); } Console.ReadKey(); } }
Need to implement callback functionalities.
Need to support Redo and Undo functionality for commands.
Sending requests to different receivers which can handle it in different ways.
Need for auditing and logging of all changes via commands.