Pattern Design Design Pattern 2 — Questions and Answers
Question 1: Which design pattern ensures that adding new product families to a system requires no changes to existing client code?
- Factory Method
- Abstract Factory (Correct answer)
- Prototype
- Builder
Correct answer: Abstract Factory
Abstract Factory provides an interface for creating families of related objects, so new families can be introduced without altering client code.
Question 2: In the Decorator pattern, what is the key structural requirement that allows decorators to be stacked?
- Decorators must extend a concrete class
- Decorators implement the same interface as the component they wrap (Correct answer)
- Decorators inherit directly from each other
- Decorators store the wrapped object as a static field
Correct answer: Decorators implement the same interface as the component they wrap
Decorators implement the same interface as the wrapped component, enabling them to be composed in any order and depth.
Question 3: Which pattern separates an algorithm's skeleton from its specific steps so subclasses can override individual steps?
- Strategy
- Command
- Template Method (Correct answer)
- Chain of Responsibility
Correct answer: Template Method
Template Method defines the overall algorithm structure in a base class while deferring specific steps to subclasses.
Question 4: A system needs to convert XML data into JSON format without changing either the XML reader or the JSON writer. Which pattern best applies?
- Facade
- Adapter (Correct answer)
- Bridge
- Proxy
Correct answer: Adapter
The Adapter pattern translates one interface into another, allowing incompatible interfaces like an XML reader and JSON writer to work together.
Question 5: What distinguishes the Flyweight pattern from simple object caching?
- Flyweight only works with immutable objects
- Flyweight separates intrinsic (shared) state from extrinsic (contextual) state (Correct answer)
- Flyweight always uses a singleton registry
- Flyweight requires objects to implement Cloneable
Correct answer: Flyweight separates intrinsic (shared) state from extrinsic (contextual) state
Flyweight explicitly splits state into intrinsic shared state (stored in the flyweight) and extrinsic state (passed by the caller) to minimize memory.
Question 6: In the Observer pattern, what problem arises if observers are not properly deregistered?
- The subject stops notifying remaining observers
- Memory leaks occur because the subject holds references to stale observers (Correct answer)
- New observers cannot be added after one is removed
- The subject converts to a singleton automatically
Correct answer: Memory leaks occur because the subject holds references to stale observers
If observers are never removed, the subject retains references to them indefinitely, preventing garbage collection and causing memory leaks.
Question 7: Which pattern provides a simplified interface to a complex subsystem without changing the subsystem's code?
- Adapter
- Mediator
- Facade (Correct answer)
- Bridge
Correct answer: Facade
Facade wraps a complex subsystem with a higher-level interface, making it easier to use without modifying the subsystem itself.
Which design pattern ensures that adding new product families to a system requires no changes to existing client code?