Bird
Raised Fist0

You are designing a text editor with undo functionality using the Command pattern. Which design choice best supports undo operations efficiently?

hard📝 Trade-off Q15 of Q15
LLD - Behavioral Design Patterns — Part 1
You are designing a text editor with undo functionality using the Command pattern. Which design choice best supports undo operations efficiently?
AStore a history stack of Command objects and call an <code>undo()</code> method on the last command
BKeep a log of all text changes as strings and replay them to undo
CUse a single Command object that modifies text directly without history
DImplement undo by reloading the entire document from disk
Step-by-Step Solution
Solution:
  1. Step 1: Understand undo with Command pattern

    Each Command object should implement both execute() and undo() methods to reverse its action.
  2. Step 2: Evaluate design choices

    Storing a history stack of Command objects allows calling undo() on the last command efficiently. Other options either lack command encapsulation or are inefficient.
  3. Final Answer:

    Store a history stack of Command objects and call an undo() method on the last command -> Option A
  4. Quick Check:

    Undo = command history stack with undo() [OK]
Quick Trick: Undo needs command history with undo method [OK]
Common Mistakes:
MISTAKES
  • Using string logs instead of command objects
  • Not implementing undo in commands
  • Reloading entire document is inefficient

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes