Pattern Design Basic Design Pattern 4 — Questions and Answers
Question 1: What is the key difference between the Strategy and Template Method patterns?
- Strategy uses inheritance; Template Method uses composition
- Strategy changes the whole algorithm; Template Method changes only steps (Correct answer)
- Strategy works at compile time; Template Method works at runtime
- Strategy is structural; Template Method is creational
Correct answer: Strategy changes the whole algorithm; Template Method changes only steps
Strategy replaces an entire algorithm via composition, while Template Method fixes the algorithm's skeleton in a base class and lets subclasses override individual steps.
Question 2: The Chain of Responsibility pattern is most appropriate when:
- Multiple objects may handle a request and the handler isn't known a priori (Correct answer)
- A single object must coordinate multiple subsystems
- An algorithm must be split across multiple classes
- Objects must be notified of state changes
Correct answer: Multiple objects may handle a request and the handler isn't known a priori
Chain of Responsibility passes a request along a chain of handlers until one processes it, decoupling senders from receivers.
Question 3: Which pattern provides a way to access elements of an aggregate object sequentially without exposing its internal representation?
- Visitor
- Memento
- Iterator (Correct answer)
- Mediator
Correct answer: Iterator
The Iterator pattern abstracts traversal logic into an iterator object, so clients can loop over a collection without knowing its underlying structure.
Question 4: In the Mediator pattern, what role does the mediator object play?
- It caches expensive computations for multiple objects
- It centralizes communication between colleague objects (Correct answer)
- It provides a unified interface to subsystems
- It stores and restores object state
Correct answer: It centralizes communication between colleague objects
The Mediator encapsulates how a set of objects interact, promoting loose coupling by keeping objects from referring to each other directly.
Question 5: What is the intent of the Memento pattern?
- Share expensive object state across instances
- Capture and externalize an object's state so it can be restored later (Correct answer)
- Define a grammar for a language and interpret sentences
- Allow incompatible interfaces to work together
Correct answer: Capture and externalize an object's state so it can be restored later
The Memento pattern saves a snapshot of an object's internal state outside the object so it can be restored to that state later without violating encapsulation.
Question 6: Abstract Factory differs from Factory Method primarily in that Abstract Factory:
- Returns a single product; Factory Method returns a family of products
- Creates families of related products; Factory Method creates one product (Correct answer)
- Uses object composition; Factory Method uses class inheritance only
- Is always implemented as a Singleton; Factory Method is not
Correct answer: Creates families of related products; Factory Method creates one product
Abstract Factory produces families of related or dependent objects through multiple factory methods, while Factory Method defines one creation method for a single product type.
Question 7: Which pattern would best decouple a UI layer from business logic by having UI elements communicate only through a single coordinator?
- Observer
- Mediator (Correct answer)
- Facade
- Proxy
Correct answer: Mediator
The Mediator centralizes control and communication, so UI components don't reference each other directly, reducing coupling across the interface.
What is the key difference between the Strategy and Template Method patterns?