Command Design Pattern

Command design pattern is part of the Behavioral Design Patterns from Gang of Four (GoF) Design Patterns. Behavioral Design patterns are concerned with communication between objects. It is used to cover/envelop a request as an object and pass it to a user. The user doesn’t know how to service the request but can use an encapsulated command object to perform the action.

In command Design Pattern there are five components involved:

  1. Command-It is an interface with the execute method. It acts as a contract.
  2. Client-Client instantiates a concrete command object and associates it with a receiver.
  3. User (Invoker) - This instructs the command to perform an action.
  4. Concrete Command - Associates a binding between receiver and action.
  5. Receiver - It is the object that knows the actual steps to perform the action.

Let us discuss it with an example; suppose power-on and power-off are the commands to turn on/turn off air conditioners. These commands are received by air conditioners. You will issue these commands using the remote controller who acts as a invoker. Client is the person who uses this remote control.

The advantage of this invoker is decoupled by the action performed by the receiver. The invoker has no knowledge of the receiver. The invoker issues a command wherein the command performs the action on a receiver. The invoker doesn’t know the details of the action being performed. So, changes to the receiver action don’t affect the invoker’s action.

Let us discuss this with code.

  1. public interface Execution  
  2.    {  
  3.        void execute();     
Inheriting this interface and making two methods, Ac_on and Ac_off.
  1. public class Ac_on: Execution  
  2.     {  
  3.         private airconditonar airc;  
  4.         public poweron(airconditonar airc)  
  5.         {  
  6.             this.airc = airc;  
  7.         }  
  8.         public void execute()  
  9.         {  
  10.             airc.on();  
  11.         }  
  12.   
  13.       
  14.     }  
  1. public class Ac_off : Execution  
  2.    {  
  3.        private airconditonar airc;  
  4.        public poweroff(airconditonar airc)  
  5.        {  
  6.            this.airc = airc;  
  7.        }  
  8.        public void execute()  
  9.        {  
  10.            airc.off();  
  11.        }  
  12.   
  13.   
  14.    }  
  1. public class airconditonar {  
  2.   
  3.   
  4.         public void on()  
  5.         {  
  6.   
  7.             Console.WriteLine("Ac is on");  
  8.         }  
  9.         public void off()  
  10.         {  
  11.   
  12.             Console.WriteLine("Ac is off");  
  13.         }  
  14.       
  15.       
  16.       
  17.     }  
Now, AC remote is the invoker who can issue several commands,
  1. public class Acremote  
  2.     {  
  3.   
  4.         private command command;  
  5.         public Acremote(command command)  
  6.         {  
  7.             this.command = command;  
  8.         }  
  9.         public void pressbutton()  
  10.         {  
  11.   
  12.             command.execute();  
  13.         }  
  14.     }  
Finally, a person who hit the command on an invoker:
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Acremote control = new Acremote();  
  6.             airconditonar ac1 = new airconditonar();  
  7.             Ac_on pon = new poweron(ac1);  
  8.             control.pressbutton();  
  9.             Ac_off poff = new poweron(ac1);  
  10.             control.pressbutton();  
  11.         }  
  12.     }