0% completed
The Command pattern is a behavioral design pattern that encapsulates a request as an object, allowing users to parameterize clients with queues, requests, and operations. It transforms a request into an independent object with all the request's details. This separation enables more versatile operations such as queuing, logging, undoing actions, and delaying execution.
Consider an online shopping platform, which is a well-known experience for many people. You may be browsing, placing things in your cart, deciding to cancel your order, or perhaps changing your mind and exchanging an item for another. Without the proper system in place, these actions might turn into a chaotic mess for both the user and the underlying software architecture.
Applying the command pattern to it converts every action into a command object, such as adding, removing, or modifying an order. This object contains all of the components required to complete the task.
A practical example of a Command Pattern is a smart home application. This application allows you to control different electronic devices, like TVs, lights, and air conditioners, with your phone.
The app presents a simple, user-friendly interface with buttons for every device or action. Each button acts as a command. Pressing a button sends a command to the corresponding device. Using the command pattern, it's easy to add some new devices to this app, hence increasing the flexibility of the application.
The command pattern consists of the following key components:
This example demonstrates a simple light control system using the Command pattern. The system can turn a light on and off using commands.
Interface Command { Method execute() } Class Light { Method turnOn() { Print "Light is ON" } Method turnOff() { Print "Light is OFF" } } Class TurnOnLightCommand implements Command { Private light: Light Constructor(light: Light) { this.light = light } Method execute() { light.turnOn() } } Class TurnOffLightCommand implements Command { Private light: Light Constructor(light: Light) { this.light = light } Method execute() { light.turnOff() } } Class RemoteControl { Private command: Command Method setCommand(command: Command) { this.command = command } Method pressButton() { command.execute() } } // Client Code Main { light = new Light() turnOn = new TurnOnLightCommand(light) turnOff = new TurnOffLightCommand(light) remote = new RemoteControl() remote.setCommand(turnOn) remote.pressButton() remote.setCommand(turnOff) remote.pressButton() }
Pros | Cons |
---|---|
Decoupling of Actions and Actors<br>Decouples the objects that send commands from the ones that perform them. | Complexity<br>It can increase the number of classes and complexity of the system. |
Ease of Extending Commands<br>New commands can be added without modifying the existing commands. | Overhead<br>Each action might require a new command class, that increases overhead. |
Simplifying Complex Operations<br>By breaking the complex operations into simpler commands, operations can be simplified. | Indirect Execution<br>Commands introduce an additional layer of abstraction, which can complicate understanding and debugging. |
Support for Undo/Redo<br>Facilitates implementing undo/redo mechanisms. |
The command pattern is a very useful tool in your design pattern toolbox. It's like possessing a magic wand that neatly condenses complex requests into small, doable objects. This leads to a world of possibilities, including the ability to undo functions, log actions, and much more, and make your code cleaner and more organized.
.....
.....
.....