Pattern Design Behavioral Design Patterns 2 — Questions and Answers
Question 1: The Mediator pattern reduces coupling between components by:
- Centralizing communication through a mediator object (Correct answer)
- Using events and callbacks only
- Delegating behavior to strategy objects
- Making all objects observers of each other
Correct answer: Centralizing communication through a mediator object
The Mediator prevents direct communication between components, routing all interactions through a central mediator that coordinates their behavior.
Question 2: The Memento pattern is used to:
- Capture and restore an object's internal state without violating encapsulation (Correct answer)
- Notify observers of state changes
- Define an object's behavior based on its current state
- Queue and log operations
Correct answer: Capture and restore an object's internal state without violating encapsulation
Memento saves a snapshot of an object's state that can later be restored, without exposing that state to outside objects.
Question 3: The State pattern is most appropriate when:
- An object's behavior must change based on its internal state (Correct answer)
- A group of objects must be notified of changes
- An algorithm must be swappable at runtime
- Requests must be processed by a chain of handlers
Correct answer: An object's behavior must change based on its internal state
The State pattern allows an object to alter its behavior when its internal state changes, making it appear to change its class.
Question 4: The Visitor pattern is useful when you need to:
- Add new operations to a class hierarchy without modifying those classes (Correct answer)
- Add new classes to a hierarchy without modifying existing operations
- Cache expensive computations across a hierarchy
- Traverse a tree structure with an iterator
Correct answer: Add new operations to a class hierarchy without modifying those classes
Visitor lets you define new operations on elements of an object structure by placing the operation logic in a visitor class rather than in the element classes.
Question 5: The Interpreter pattern is primarily used to:
- Define a grammar and interpret sentences in a language (Correct answer)
- Translate requests between incompatible interfaces
- Walk a composite tree structure
- Manage complex state transitions
Correct answer: Define a grammar and interpret sentences in a language
The Interpreter pattern provides a way to evaluate sentences in a formal language by representing each grammar rule as a class.
Question 6: In the Observer pattern, a 'pull' model means:
- Observers query the subject for data after receiving a notification (Correct answer)
- The subject pushes all data to observers with each notification
- Observers are proactively polled by the subject
- The subject stores a complete snapshot for each observer
Correct answer: Observers query the subject for data after receiving a notification
In the pull model, the notification carries minimal information and each observer fetches only the data it needs from the subject.
The Mediator pattern reduces coupling between components by: