Pattern Design Design Pattern 5 — Questions and Answers
Question 1: In the Visitor pattern, what mechanism allows new operations to be added without modifying the element classes?
- Elements call accept(visitor), which dispatches to the correct visitor method via double dispatch (Correct answer)
- Elements inherit from a common visitor base class
- The visitor uses reflection to inspect element types at runtime
- New visitor subclasses override the element's methods directly
Correct answer: Elements call accept(visitor), which dispatches to the correct visitor method via double dispatch
Double dispatch — the element calls accept(visitor) and the visitor's overloaded visit() method selects the correct operation based on the element's actual type.
Question 2: Why is the Singleton pattern considered an anti-pattern in test-driven development?
- Singletons always cause memory leaks in test environments
- Singletons introduce global shared state, making tests order-dependent and difficult to isolate (Correct answer)
- Singletons cannot be instantiated inside unit test frameworks
- Singletons prevent the use of mocking libraries
Correct answer: Singletons introduce global shared state, making tests order-dependent and difficult to isolate
A singleton's global state persists between tests, causing tests to interfere with each other and making isolation and mocking difficult.
Question 3: Which pattern would you choose to implement an event system where multiple listeners react to different event types published by a single source?
- Chain of Responsibility
- Observer (Correct answer)
- Mediator
- Command
Correct answer: Observer
Observer (publish-subscribe) allows multiple listeners to register for events from a single subject and be notified automatically when events occur.
Question 4: An application uses a third-party library that returns temperature in Celsius, but all internal components expect Fahrenheit. Which pattern resolves this with minimal code changes?
- Proxy
- Decorator
- Adapter (Correct answer)
- Bridge
Correct answer: Adapter
Adapter wraps the third-party library and converts Celsius output to Fahrenheit, making the library compatible with the internal interface.
Question 5: What distinguishes a creational design pattern from a structural one?
- Creational patterns define how objects are created; structural patterns define how objects are composed or connected (Correct answer)
- Creational patterns apply only to interfaces; structural patterns apply only to classes
- Creational patterns reduce memory use; structural patterns improve performance
- Creational patterns are language-specific; structural patterns are language-agnostic
Correct answer: Creational patterns define how objects are created; structural patterns define how objects are composed or connected
Creational patterns focus on object instantiation mechanisms, while structural patterns focus on how classes and objects are assembled into larger structures.
Question 6: In the Interpreter pattern, what does each grammar rule in the language correspond to?
- A singleton instance
- A concrete class implementing an expression interface (Correct answer)
- A method in a single large interpreter class
- A configuration file entry
Correct answer: A concrete class implementing an expression interface
Each grammar rule maps to a concrete expression class with an interpret() method, together forming a composite expression tree.
Question 7: Which combination of patterns is commonly used to implement an undo/redo history stack?
- Singleton and Observer
- Command and Memento (Correct answer)
- Strategy and Decorator
- Composite and Iterator
Correct answer: Command and Memento
Command encapsulates each action with an undo method, and Memento captures object state snapshots, together enabling a robust undo/redo history.
In the Visitor pattern, what mechanism allows new operations to be added without modifying the element classes?