Software Design Patterns and Principles - Part 10 (Command Design Pattern)
Saiful Islam Rasel
Senior Engineer, SDE @ bKash | Ex: AsthaIT | Sports Programmer | Problem Solver | FinTech | Microservice | Java | Spring-boot | C# | .NET | PostgreSQL | DynamoDB | JavaScript | TypeScript | React.js | Next.js | Angular
**** Previous Parts ****
Part 1: Introduction
Story:
Refactoring guru website gives very nice real world analogy, like:
After a long walk through the city, you get to a nice restaurant and sit at the table by the window. A friendly waiter approaches you and quickly takes your order, writing it down on a piece of paper. The waiter goes to the kitchen and sticks the order on the wall. After a while, the order gets to the chef, who reads it and cooks the meal accordingly. The cook places the meal on a tray along with the order. The waiter discovers the tray, checks the order to make sure everything is as you wanted it, and brings everything to your table.
Command Design Pattern:
Definition:
Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable?operations.
As the definition stat, Command design pattern suggest a method as a object approach. Means turns a request into a stand-alone object. This stand-alone object can be executed anytime with delay, can be queued, can be redo/undo as well.
Problem:
In the above story, you just place your choice and waiter just write it down in a paper. But how a paper order turns into a real meal? How different types of order and different types of paper writings turn into meal after cook?
The Command Design Pattern is incredibly versatile and can be applied in various scenarios across different domains. Here are some common scenarios where the Command Design Pattern can be used effectively:
领英推荐
These are just a few examples of scenarios where the Command Design Pattern can be applied effectively. Its flexibility and versatility make it a valuable tool for designing modular, extensible, and maintainable software systems.
Solution:
In the above story, the paper order serves as a command. It remains in a queue until the chef is ready to serve it. The order contains all the relevant information required to cook the meal. It allows the chef to start cooking right away instead of running around clarifying the order details from you directly.
We can also use or implement command pattern as a solution of the problems stated earlier sections as well.
UML Diagram:
Here in UML diagram we can see that, Clients interacts basically with the Invoker class for invoke/execute the command. And all ConcreteCommand class implements the Command interface. When Invoker invoke the command one ConcreteCommand class then propagate the Receiver class Action().
When to use:
Implementation:
// Define the Command interface
interface Command {
void execute();
}
// Concrete command classes
class LightOnCommand implements Command {
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOn();
}
}
class LightOffCommand implements Command {
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOff();
}
}
// Receiver class
class Light {
public void turnOn() {
System.out.println("Light is on");
}
public void turnOff() {
System.out.println("Light is off");
}
}
// Invoker class
class RemoteControl {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
command.execute();
}
}
// Client class
public class CommandPatternExample {
public static void main(String[] args) {
// Create the receiver
Light light = new Light();
// Create concrete command objects
Command lightOnCommand = new LightOnCommand(light);
Command lightOffCommand = new LightOffCommand(light);
// Create invoker
RemoteControl remote = new RemoteControl();
// Associate commands with invoker
remote.setCommand(lightOnCommand);
remote.pressButton(); // Output: Light is on
remote.setCommand(lightOffCommand);
remote.pressButton(); // Output: Light is off
}
}
Another C# implementation of Command design pattern.
Achieved Design Principles:
The Command Design Pattern helps achieve several design principles and patterns, which contribute to building robust, flexible, and maintainable software systems. Here are some of the key design principles achieved by using the Command Design Pattern:
By adhering to these design principles, the Command Design Pattern helps developers create software systems that are modular, extensible, and easy to maintain, leading to improved code quality and developer productivity.
Happy Learning !!!
Happy Coding !!!
Happy Programming !!!
Senior Engineer, SDE @ bKash | Ex: AsthaIT | Sports Programmer | Problem Solver | FinTech | Microservice | Java | Spring-boot | C# | .NET | PostgreSQL | DynamoDB | JavaScript | TypeScript | React.js | Next.js | Angular
6 个月Previous Parts: Part 1:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-1-saiful-islam-rasel-eqfec/ Part 2:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-2-list-most-rasel-kepmc/ Part 3:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-3-factory-method-rasel-ir4tc/ Part 4:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-4-builder-pattern-rasel-41cmc/ Part 5:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-5-adapter-pattern-rasel-nfjrc/ Part 6:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-6-decorator-rasel-ijxwc/ Part 7:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-7-composite-rasel-tuzfc/ Part 8:?https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-8-fa%2525C3%2525A7ade-pattern-rasel-6bucc/ Part 9: https://www.dhirubhai.net/pulse/software-design-patterns-principles-part-9-proxy-pattern-rasel-ajmgc/