The Observer pattern lets one object (called the subject) notify many other objects (called observers) automatically when its state changes. It helps keep objects updated without tight coupling.
Click to reveal answer
beginner
How does the Strategy pattern help in software design?
The Strategy pattern allows selecting an algorithm’s behavior at runtime by defining a family of algorithms and making them interchangeable. It helps change how something works without changing the code that uses it.
Click to reveal answer
beginner
Explain the Command pattern in simple terms.
The Command pattern turns a request or action into an object. This lets you store, queue, or undo actions easily by treating commands as objects.
Click to reveal answer
intermediate
Which behavioral pattern helps in decoupling the sender of a request from its receiver by using objects to represent actions?
The Command pattern helps decouple the sender and receiver by encapsulating requests as objects, allowing flexible command management like undo, queue, or logging.
Click to reveal answer
intermediate
Why is the Observer pattern useful in user interface design?
Because it allows UI elements to automatically update when the data they display changes, without the UI needing to constantly check for updates. This keeps the interface responsive and consistent.
Click to reveal answer
Which pattern allows an object to notify multiple other objects about changes?
AObserver
BStrategy
CCommand
DSingleton
✗ Incorrect
The Observer pattern is designed to notify multiple observers when the subject changes.
What does the Strategy pattern primarily help with?
ACreating only one instance of a class
BNotifying objects of changes
CChanging algorithms at runtime
DEncapsulating requests as objects
✗ Incorrect
Strategy pattern allows selecting and changing algorithms dynamically.
Which pattern encapsulates a request as an object to support undo operations?
AObserver
BStrategy
CFactory
DCommand
✗ Incorrect
The Command pattern encapsulates requests as objects, enabling undo and other features.
In the Observer pattern, what are the objects that receive updates called?
AObservers
BSubjects
CCommands
DStrategies
✗ Incorrect
Observers are the objects that get notified when the subject changes.
Which pattern would you use to switch between different ways of performing a task without changing the task's code?
AObserver
BStrategy
CCommand
DAdapter
✗ Incorrect
Strategy pattern allows changing the algorithm or behavior without modifying the client code.
Describe the Observer pattern and give a real-life example where it might be used.
Think about how a news app updates you when new articles arrive.
You got /5 concepts.
Explain how the Command pattern can help implement undo functionality in software.
Consider how a text editor remembers your actions to undo them.
You got /4 concepts.
Practice
(1/5)
1. Which behavioral pattern is used when an object needs to notify multiple other objects about a change in its state?
easy
A. Command
B. Strategy
C. Observer
D. Singleton
Solution
Step 1: Understand the role of Observer pattern
The Observer pattern allows one object (the subject) to notify many dependent objects (observers) automatically when its state changes.
Step 2: Compare with other patterns
Strategy selects algorithms at runtime, Command encapsulates requests, and Singleton restricts instance creation. Only Observer fits the notification role.
Final Answer:
Observer -> Option C
Quick Check:
Notification to many = Observer [OK]
Hint: Notification to many objects? Think Observer pattern [OK]
Common Mistakes:
Confusing Strategy with Observer
Thinking Command handles notifications
Mixing Singleton with behavioral patterns
2. Which of the following is the correct way to define a Command pattern interface in object-oriented design?
easy
A. An interface with a method like execute() to perform the action
B. A class that holds multiple algorithms to choose from
C. A subject that notifies observers about changes
D. A singleton class that manages global state
Solution
Step 1: Identify Command pattern structure
The Command pattern encapsulates a request as an object, typically by defining an interface with an execute() method to perform the action.
Step 2: Eliminate other options
A class that holds multiple algorithms to choose from describes Strategy, C describes Observer, and D describes Singleton, none of which define a command interface.
Final Answer:
An interface with a method like execute() to perform the action -> Option A
Quick Check:
Command interface = execute() method [OK]
Hint: Command pattern always has an execute() method [OK]
Common Mistakes:
Confusing Command with Strategy interface
Thinking Command notifies observers
Mixing Singleton with Command
3. Consider this code snippet using the Strategy pattern:
class Context:
def __init__(self, strategy):
self.strategy = strategy
def execute(self, data):
return self.strategy.do_algorithm(data)
class StrategyA:
def do_algorithm(self, data):
return sorted(data)
class StrategyB:
def do_algorithm(self, data):
return sorted(data, reverse=True)
context = Context(StrategyA())
result = context.execute([3, 1, 2])
print(result)
What will be the output?
medium
A. [3, 1, 2]
B. [1, 2, 3]
C. [3, 2, 1]
D. TypeError
Solution
Step 1: Understand the Strategy pattern usage
The Context uses StrategyA, whose do_algorithm method sorts the list in ascending order.
Hint: StrategyA sorts ascending, so output is sorted list [OK]
Common Mistakes:
Assuming StrategyB is used
Confusing sorting order
Expecting original list unchanged
4. In the Observer pattern, what is the main issue if observers are not properly detached when no longer needed?
medium
A. Memory leaks due to lingering references
B. Strategy selection becomes unpredictable
C. Commands fail to execute
D. Singleton instance is duplicated
Solution
Step 1: Understand observer detachment importance
If observers are not detached, the subject keeps references to them, preventing garbage collection.
Step 2: Identify consequence
This causes memory leaks because unused observers remain in memory unnecessarily.
Final Answer:
Memory leaks due to lingering references -> Option A
Quick Check:
Undetached observers cause memory leaks [OK]
Hint: Undetached observers cause memory leaks [OK]
Common Mistakes:
Confusing with Strategy or Command issues
Thinking it causes command execution failure
Assuming Singleton duplication
5. You want to design a system where users can perform different operations (like save, load, delete) on files, and you want to queue these operations to execute later or undo them if needed. Which behavioral pattern best fits this requirement and why?
hard
A. Singleton pattern, because it ensures only one file manager exists
B. Observer pattern, because it notifies users about file changes
C. Strategy pattern, because it selects the best file operation algorithm
D. Command pattern, because it encapsulates requests as objects allowing queuing and undo
Solution
Step 1: Analyze the requirement for queuing and undo
Queuing operations and supporting undo requires encapsulating each request as an object to store and manage them flexibly.
Step 2: Match with behavioral patterns
The Command pattern encapsulates requests as objects, enabling queuing, logging, and undo functionality. Observer and Strategy do not support queuing commands, and Singleton only controls instance creation.
Final Answer:
Command pattern, because it encapsulates requests as objects allowing queuing and undo -> Option D
Quick Check:
Queue and undo operations = Command pattern [OK]
Hint: Queue and undo operations? Use Command pattern [OK]
Common Mistakes:
Choosing Observer for queuing commands
Confusing Strategy with Command
Selecting Singleton for operation management
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.