Pattern Design Ultimate Design Pattern 4 — Questions and Answers
Question 1: In the Strategy pattern, what is the relationship between the Context and the Strategy interface?
- Context inherits from Strategy
- Context holds a reference to a Strategy object and delegates behavior to it (Correct answer)
- Strategy is a static utility used by Context
- Context and Strategy share a common base class
Correct answer: Context holds a reference to a Strategy object and delegates behavior to it
Context aggregates a Strategy via composition and delegates the algorithm to it, making the algorithm easily swappable at runtime.
Question 2: Which pattern provides a surrogate or placeholder for another object to control access to it?
- Decorator
- Proxy (Correct answer)
- Adapter
- Facade
Correct answer: Proxy
The Proxy pattern wraps an object to control access, add lazy initialization, logging, caching, or remote communication.
Question 3: What distinguishes the Abstract Factory pattern from the Factory Method pattern?
- Factory Method uses inheritance; Abstract Factory uses composition to produce families of related objects (Correct answer)
- Abstract Factory creates a single product; Factory Method creates multiple
- They are the same pattern with different names
- Factory Method requires multiple concrete factories
Correct answer: Factory Method uses inheritance; Abstract Factory uses composition to produce families of related objects
Abstract Factory provides an interface for creating families of related objects, while Factory Method delegates creation of a single product to subclasses via inheritance.
Question 4: Which design pattern is best suited when you need to decouple many-to-many communications between objects by centralizing all interaction through one object?
- Observer
- Mediator (Correct answer)
- Command
- Facade
Correct answer: Mediator
The Mediator pattern reduces chaotic dependencies between many objects by routing all communication through a central mediator object.
Question 5: In which scenario is the Builder pattern most appropriate compared to a simple constructor?
- When the object has no fields
- When creating an object requires many optional parameters or a multi-step construction process (Correct answer)
- When the object is immutable
- When you need exactly one instance of an object
Correct answer: When creating an object requires many optional parameters or a multi-step construction process
Builder shines when constructing complex objects step by step, avoiding telescoping constructors or partially initialized objects.
Question 6: What is the key difference between Decorator and Inheritance when extending behavior?
- Decorator changes the object's type; inheritance does not
- Decorator wraps objects at runtime; inheritance extends at compile time — Decorator is more flexible (Correct answer)
- Inheritance allows multiple wrappers; Decorator does not
- There is no meaningful difference
Correct answer: Decorator wraps objects at runtime; inheritance extends at compile time — Decorator is more flexible
Decorator composes behavior dynamically at runtime by wrapping objects, avoiding the class explosion that deep inheritance hierarchies cause.
Question 7: Which pattern is used to represent grammar rules and interpret sentences in a language?
- Command
- Interpreter (Correct answer)
- Visitor
- State
Correct answer: Interpreter
The Interpreter pattern defines a grammar and provides an interpreter to process sentences in that language, often using a composite expression tree.
In the Strategy pattern, what is the relationship between the Context and the Strategy interface?