Command Design Pattern is to decouple an action from its action invoker. This is achieved by introducing a new Command layer between the action and the invoker. Action creates inside the receiver and there can be multiple actions in a single receiver class. Invoker is invoking a single function by calling a new command layer without knowing receiver actions.
In Above “Invoker is invoking single function” means Invoker is executing a command. Once the invoker executes the command then the command executes the appropriate action inside the receiver. Invoker invokes command without knowing details of receiver’s action. it just executes the command and the middle command execute the appropriate actions.
Command Design Pattern comes under behavioral patterns(Design Pattern) and encapsulated information about actions. That means changes done to the receiver’s actions do not affect with invoker. It’s because of command layer decouples the invoker and action.
There are three main components in Command Design Pattern, but a command (interface/Abstract class) has concrete implementations. Those components are,
- Receiver: Which is executing some real actions.
- Command interface/abstract class: Declaring command to execute an action in Receiver
- Concrete command Implementations: Implementation of Command interface/abstract class which is perform the Receiver’s action by keeping reference with the Receiver
- Invoker: just invoke the command to perform some work.
Let’s move into an Example of how these components map with Real World objects.
Command Design Pattern Real World Example
Let’s take a simple example of a color light system handling traffic in a junction. There is a traffic control center that gives commands according to the traffic. Panel colors change when according to the given command. The Control center just gives commands without knowing color patterns, but the middle command executes the correct action. As a result, colors are visible accordingly.
In this case, the traffic control center is the invoker, and Color panel lights are the receivers. Processing and executing the command by command layer which encapsulates data between light actions and traffic control center.
There are three colors in the panel Red, yellow, and green. The traffic control center gives four types of commands to invoke one at a time. Consider those commands Go, Stop, Ready to go, and Ready to stop. Let’s move on to the class diagram.
Command Design Pattern in Java
Class diagram design showing four implementations of commands, three colors, and a traffic control invoker. Let’s move into the implementation of the class diagram.
//First create Receiver classes //GreenLight class Includes Green light related functinalies(turnOn/turnOff) class GreenLight{ public void turnOn() { System.out.println("Showing Green Light"); } public void turnOff() { System.out.println("Hiding Green Light"); } } //YellowLight class Includes Yellow light related functinalies(turnOn/turnOff) class YellowLight{ public void turnOn() { System.out.println("Showing Yellow Light"); } public void turnOff() { System.out.println("Hiding Yellow Light"); } } //RedLight class Includes Red light related functinalies(turnOn/turnOff) class RedLight{ public void turnOn() { System.out.println("Showing Red Light"); } public void turnOff() { System.out.println("Hiding Red Light"); } } // Then create Command interface with execute command abstract class Command { protected YellowLight yellowLight; protected RedLight redLight; protected GreenLight greenLight; public Command(YellowLight yellowLight, RedLight redLight, GreenLight greenLight) { this.yellowLight = yellowLight; this.redLight = redLight; this.greenLight = greenLight; } abstract void execute(); } // There are four Concrete commands class class GoCommand extends Command { public GoCommand( YellowLight yellowLight,RedLight redLight,GreenLight greenLight) { super(yellowLight ,redLight,greenLight); } @Override public void execute() { greenLight.turnOn(); redLight.turnOff(); yellowLight.turnOff(); } } class StopCommand extends Command { public StopCommand( YellowLight yellowLight,RedLight redLight,GreenLight greenLight) { super(yellowLight ,redLight,greenLight); } @Override public void execute() { redLight.turnOn(); yellowLight.turnOff(); greenLight.turnOff(); } } class ReadyToStopCommand extends Command { public ReadyToStopCommand( YellowLight yellowLight,RedLight redLight,GreenLight greenLight) { super(yellowLight ,redLight,greenLight); } public void execute() { yellowLight.turnOn(); redLight.turnOff(); greenLight.turnOff(); } } class ReadyToGoCommand extends Command { public ReadyToGoCommand( YellowLight yellowLight,RedLight redLight,GreenLight greenLight) { super(yellowLight ,redLight,greenLight); } public void execute() { yellowLight.turnOn(); redLight.turnOn(); greenLight.turnOff(); } } class TrafficControlCenter { Command command; //Setting runtime command public void setCommand(Command command) { this.command = command; } //Invoke appropriate command public void executeCommand() { command.execute(); } } public class TrafficControlCommandClient { public static void main(String[] args) { GreenLight greenLight = new GreenLight(); //receiver 1 Green Light RedLight redLight = new RedLight(); //receiver 2 Red Light YellowLight yellowLight = new YellowLight(); //receiver 3 Yellow Light TrafficControlCenter controlCenter=new TrafficControlCenter(); //Invoker System.out.println("Stop Command"); controlCenter.setCommand( new StopCommand( yellowLight ,redLight,greenLight )); controlCenter.executeCommand(); System.out.println(""); System.out.println("Ready To Go Command "); controlCenter.setCommand( new ReadyToGoCommand( yellowLight ,redLight,greenLight)); controlCenter.executeCommand(); System.out.println(""); System.out.println("Go Command"); controlCenter.setCommand( new GoCommand( yellowLight ,redLight,greenLight )); controlCenter.executeCommand(); System.out.println(""); System.out.println("Ready To Stop Command"); controlCenter.setCommand( new ReadyToStopCommand( yellowLight ,redLight,greenLight )); controlCenter.executeCommand(); } }
Output
Stop Command Showing Red Light Hiding Yellow Light Hiding Green Light Ready To Go Command Showing Yellow Light Showing Red Light Hiding Green Light Go Command Showing Green Light Hiding Red Light Hiding Yellow Light Ready To Stop Command Showing Yellow Light Hiding Red Light Hiding Green Light
According to the Command Design Pattern traffic control system is implemented and this is not the real way of handling traffic control, but this is just a demonstration of traffic control.
There are four commands and commands executing the color lights correctly.
Advantages of Command Design Pattern
- It’s easy to extend by adding commands or receivers without affecting existing code and functionality. Consider the need to add a Blink Yellow Color Option. It’s easy to add a new command.
- Hide the implementation from clients
- Separate(decouple) Receive and invoker make code more maintainable
Once following the Command Design Pattern there needs to create many classes to achieve the problems that may disadvantage.
Design Patterns Book
Design Patterns are one of the most famous solutions in programming and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns are created based on basic OOP concepts (If your beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )
There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.
Books | Description |
---|---|
Head First Design Patterns: A Brain-Friendly Guide | My favorite and it’s more readable. There are good examples and they look good to beginners as well. |
Design Patterns: Elements of Reusable Object-Oriented Software | There are 23 design patterns. showing how to select an appropriate pattern for your case. |
Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software | There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles. |
There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.
Njfhsjdwkdjwfh jiwkdwidwhidjwi jiwkdowfiehgejikdoswfiw https://gehddijiwfugwdjaidheufeduhwdwhduhdwudw.com/fjhdjwksdehfjhejdsdefhe
Nice post. I learn something totally new and challenging on sites I stumbleupon every day. Its always interesting to read articles from other authors and practice a little something from other web sites.
Sweet website , super pattern, very clean and use genial.
Thank you for Reaching us https://ennicode.com/, If you have time, share us with your friends if you wish to receive new posts please subscribe Thanks