Bird
0
0
LLDsystem_design~25 mins

Command pattern for undo in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Undo functionality using Command Pattern
Design the command pattern structure to support undo functionality in a local application. Out of scope: redo functionality, distributed undo, or persistent undo history.
Functional Requirements
FR1: Support executing commands that change system state
FR2: Allow undoing the last executed command
FR3: Support multiple undo operations in sequence
FR4: Commands should be encapsulated as objects
FR5: Maintain a history of executed commands for undo
FR6: Ensure commands can be extended easily for new operations
Non-Functional Requirements
NFR1: Undo operations must be efficient with minimal latency
NFR2: Support up to 1000 commands in undo history
NFR3: System should be modular and easy to maintain
NFR4: Memory usage should be optimized for command history
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Command interface with execute and undo methods
Concrete command classes implementing specific actions
Invoker to execute commands and manage history
Receiver which performs the actual operations
Command history stack to track executed commands
Design Patterns
Command pattern for encapsulating requests
Stack data structure for undo history
Memento pattern for saving object state (optional)
Composite pattern if commands can be grouped
Reference Architecture
Client
  |
Invoker (Command Manager)
  |
Command Interface <---- Concrete Commands ----> Receiver
  |
Command History Stack (for undo)
Components
Command Interface
Abstract class or interface
Defines execute() and undo() methods for commands
Concrete Commands
Classes implementing Command Interface
Encapsulate specific actions and their undo logic
Invoker (Command Manager)
Class with command execution and history stack
Executes commands and manages undo stack
Receiver
Domain-specific classes
Performs actual operations requested by commands
Command History Stack
Stack data structure
Stores executed commands for undo operations
Request Flow
1. Client creates a Concrete Command with a Receiver and parameters
2. Client passes command to Invoker to execute
3. Invoker calls command.execute(), which triggers Receiver action
4. Invoker pushes command onto Command History Stack
5. When undo is requested, Invoker pops last command from stack
6. Invoker calls command.undo(), which reverses Receiver action
Database Schema
Not applicable for local command pattern design; focus is on in-memory command objects and history stack.
Scaling Discussion
Bottlenecks
Memory usage grows with large command history
Undo latency if commands have complex undo logic
Difficulty managing commands that affect shared state
Extending commands for new operations may increase complexity
Solutions
Limit command history size with a fixed max stack length
Optimize undo logic in commands for performance
Use careful synchronization if commands modify shared state
Use inheritance and interfaces to keep command extensions clean
Interview Tips
Time: Spend 10 minutes explaining the command pattern basics and undo requirements, 15 minutes designing the components and data flow, 10 minutes discussing scaling and edge cases, and 10 minutes answering questions.
Explain how command pattern encapsulates actions and undo logic
Describe the role of Invoker and command history stack
Discuss how undo reverses the last executed command
Mention handling of commands that cannot be undone
Talk about memory and performance considerations for undo history