Command Pattern
The Command pattern is a behavioral design pattern that allows you to encapsulate a request as an object, giving you the flexibility to parameterize clients with different requests, queue or log requests, and support undoable operations. By turning requests into objects, you can decouple the sender of the request from the object that executes the action, leading to more modular and extensible code. This pattern is especially useful in scenarios such as implementing UI controls, task scheduling, and macro recording, where actions need to be executed, undone, or stored for later use.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950// Command interface class Command { execute() {} undo() {} } // Receiver: the object that performs the action class Light { turnOn() { console.log("The light is ON"); } turnOff() { console.log("The light is OFF"); } } // Concrete Command: encapsulates a request to turn on the light class TurnOnLightCommand extends Command { constructor(light) { super(); this.light = light; } execute() { this.light.turnOn(); } undo() { this.light.turnOff(); } } // Invoker: the button that triggers commands class Button { constructor(command) { this.command = command; } click() { this.command.execute(); } clickUndo() { this.command.undo(); } } // Usage const livingRoomLight = new Light(); const turnOnCommand = new TurnOnLightCommand(livingRoomLight); const button = new Button(turnOnCommand); button.click(); // Output: The light is ON button.clickUndo(); // Output: The light is OFF
One of the most powerful features of the Command pattern is its support for undo and redo operations. Since each command is represented as a distinct object, you can keep a history of executed commands. To undo an action, you simply call the undo method on the last executed command object. Similarly, you can redo actions by re-executing the command. This approach makes it easy to implement complex features like multi-level undo/redo in applications such as text editors, drawing tools, or any interface where users may want to reverse or repeat their actions.
1. What is a key benefit of the Command pattern?
2. The Command pattern is especially useful for:
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain how to implement multi-level undo and redo with the Command pattern?
What are some real-world examples where the Command pattern is especially useful?
How does the Command pattern help with decoupling in software design?
Awesome!
Completion rate improved to 7.14
Command Pattern
Sveip for å vise menyen
The Command pattern is a behavioral design pattern that allows you to encapsulate a request as an object, giving you the flexibility to parameterize clients with different requests, queue or log requests, and support undoable operations. By turning requests into objects, you can decouple the sender of the request from the object that executes the action, leading to more modular and extensible code. This pattern is especially useful in scenarios such as implementing UI controls, task scheduling, and macro recording, where actions need to be executed, undone, or stored for later use.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950// Command interface class Command { execute() {} undo() {} } // Receiver: the object that performs the action class Light { turnOn() { console.log("The light is ON"); } turnOff() { console.log("The light is OFF"); } } // Concrete Command: encapsulates a request to turn on the light class TurnOnLightCommand extends Command { constructor(light) { super(); this.light = light; } execute() { this.light.turnOn(); } undo() { this.light.turnOff(); } } // Invoker: the button that triggers commands class Button { constructor(command) { this.command = command; } click() { this.command.execute(); } clickUndo() { this.command.undo(); } } // Usage const livingRoomLight = new Light(); const turnOnCommand = new TurnOnLightCommand(livingRoomLight); const button = new Button(turnOnCommand); button.click(); // Output: The light is ON button.clickUndo(); // Output: The light is OFF
One of the most powerful features of the Command pattern is its support for undo and redo operations. Since each command is represented as a distinct object, you can keep a history of executed commands. To undo an action, you simply call the undo method on the last executed command object. Similarly, you can redo actions by re-executing the command. This approach makes it easy to implement complex features like multi-level undo/redo in applications such as text editors, drawing tools, or any interface where users may want to reverse or repeat their actions.
1. What is a key benefit of the Command pattern?
2. The Command pattern is especially useful for:
Takk for tilbakemeldingene dine!