The Command Pattern encapsulates a request as an object, allowing you to parameterize clients, queue requests, and support undo/redo functionality. 🕹️
It separates what to do from who does it by using invokers, commands, and receivers.
- Command Interface → Defines an action (execute/undo).
- Concrete Commands → Implement the actions.
- Receiver → The actual executor of the request.
- Invoker → Triggers the command.
- Client → Configures and assigns commands.
✅ When you need undo/redo functionality.
✅ When you want to queue or log actions.
✅ When you want to decouple senders from receivers.
✅ When actions need to be executed at different times.
- Remote Controls: Pressing buttons to trigger actions on a TV 📺
- Text Editors: Undo/Redo functionality 📝
- Macro Recording: Recording and replaying user actions 🎬
🔗 Example Code: See Implementation