Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the Command pattern in software design?
The Command pattern is a design pattern that turns a request into a stand-alone object containing all information about the request. This allows for parameterizing methods with different requests, queuing or logging requests, and supporting undoable operations.
Click to reveal answer
intermediate
How does the Command pattern help implement undo functionality?
Each command object stores the action and its state before execution. To undo, the command calls its undo method, reversing the action using the saved state.
Click to reveal answer
beginner
What are the main components of the Command pattern?
1. Command Interface: declares execute and undo methods. 2. Concrete Command: implements the interface and defines actions. 3. Invoker: asks the command to carry out the request. 4. Receiver: knows how to perform the operations. 5. Client: creates command objects and sets receivers.
Click to reveal answer
intermediate
Why is storing command history important in undo implementations?
Storing command history allows the system to track executed commands in order. This history enables undoing commands in reverse order, ensuring correct reversal of actions.
Click to reveal answer
beginner
Give a simple real-life analogy for the Command pattern with undo.
Think of a remote control (Invoker) that sends commands to a TV (Receiver). Each button press is a command object. If you want to undo the last action, you press the undo button, which tells the remote to reverse the last command, like turning the volume back down.
Click to reveal answer
Which component in the Command pattern is responsible for executing and undoing actions?
AConcrete Command
BInvoker
CReceiver
DClient
✗ Incorrect
The Concrete Command implements the execute and undo methods to perform and reverse actions.
Why do we need to store the state before executing a command for undo?
ATo speed up execution
BTo log user activity
CTo reverse the action correctly
DTo notify the client
✗ Incorrect
Storing the state before execution allows the undo method to restore the previous state, effectively reversing the action.
What role does the Invoker play in the Command pattern?
AExecutes commands
BCreates commands
CStores command history
DPerforms the actual operation
✗ Incorrect
The Invoker asks the command to execute or undo the action but does not know the details of the operation.
In the Command pattern, what is the Receiver responsible for?
AUndoing commands
BExecuting the actual operation
CStoring commands
DInvoking commands
✗ Incorrect
The Receiver knows how to perform the actual work when a command is executed.
Which of the following is NOT a benefit of using the Command pattern for undo?
AEnables undo and redo
BSupports queuing and logging
CDecouples sender and receiver
DAutomatically optimizes performance
✗ Incorrect
The Command pattern does not automatically optimize performance; it focuses on decoupling and flexibility.
Explain how the Command pattern enables undo functionality in a software system.
Think about how each action can be reversed by storing information and calling undo.
You got /5 concepts.
Describe the roles of the main components in the Command pattern and how they interact.
Consider who creates, who calls, and who does the work.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the Command pattern in the context of undo functionality?
easy
A. To replace all conditional statements with loops
B. To directly modify the user interface without storing history
C. To store data in a database for permanent record
D. To encapsulate actions as objects with execute and undo methods
Solution
Step 1: Understand the role of Command pattern
The Command pattern wraps actions as objects, allowing them to be executed and undone independently.
Step 2: Relate to undo functionality
This wrapping enables storing commands in a history stack, so undo can call the undo method on the last command.
Final Answer:
To encapsulate actions as objects with execute and undo methods -> Option D
Quick Check:
Command pattern = encapsulate actions for undo [OK]
Hint: Command pattern wraps actions for undo/redo [OK]
Common Mistakes:
Thinking Command pattern modifies UI directly
Confusing Command pattern with data storage
Assuming it replaces loops or conditionals
2. Which method signature correctly belongs to a Command interface supporting undo?
easy
A. void save(); void load();
B. void execute(); void undo();
C. void start(); void finish();
D. void run(); void stop();
Solution
Step 1: Identify standard Command interface methods
The Command pattern typically defines an execute() method to perform the action and an undo() method to reverse it.
Step 2: Match method signatures
Only void execute(); void undo(); has execute() and undo(), matching the Command pattern for undo.
Final Answer:
void execute(); void undo(); -> Option B
Quick Check:
Command methods = execute and undo [OK]
Hint: Look for execute() and undo() methods [OK]
Common Mistakes:
Choosing unrelated method names like run/stop
Confusing start/finish with undo functionality
Assuming save/load are Command methods
3. Given the following code snippet, what will be the output after calling undo() on the last command?
Execute saves previous total and multiplies current total by value. Undo divides total by value.
Step 2: Identify problem with undo
Undo divides by value, but if value is zero or changed, this may not restore original total exactly. It should restore saved previous total instead.
Final Answer:
Undo should restore previous value, not divide -> Option C
Quick Check:
Undo must restore saved state, not recalculate [OK]
Hint: Undo must restore saved state, not recalculate [OK]
Common Mistakes:
Assuming division always reverses multiplication
Not saving previous state before execute
Ignoring edge cases like zero multiplication
5. You are designing a text editor with undo using the Command pattern. Which approach best supports multiple undo and redo operations efficiently?
hard
A. Use two stacks: one for undo commands, one for redo commands
B. Store all commands in a single list without pointers
C. Only keep the last command for undo, discard others
D. Save full document snapshots after each command
Solution
Step 1: Understand undo/redo requirements
Undo reverses last command, redo reapplies commands undone. Efficient support requires tracking both undo and redo history.
Step 2: Evaluate data structures
Two stacks allow pushing commands on execute, popping for undo, and pushing undone commands to redo stack. This supports multiple undo/redo efficiently.
Final Answer:
Use two stacks: one for undo commands, one for redo commands -> Option A
Quick Check:
Two stacks = efficient undo/redo [OK]
Hint: Two stacks handle undo and redo efficiently [OK]