Pattern Design Ultimate Design Pattern 3 — Questions and Answers
Question 1: The Bridge pattern separates an abstraction from its implementation so that the two can vary independently. What is the core structural mechanism?
- Inheritance hierarchy only
- Composition — the abstraction holds a reference to the implementation (Correct answer)
- Static method delegation
- Interface default methods
Correct answer: Composition — the abstraction holds a reference to the implementation
Bridge uses composition: the abstraction contains a reference to an implementor object, decoupling the two hierarchies.
Question 2: In a creational context, which pattern creates objects by copying an existing prototype instead of constructing from scratch?
- Builder
- Abstract Factory
- Prototype (Correct answer)
- Factory Method
Correct answer: Prototype
The Prototype pattern clones an existing object, which is useful when instantiation is expensive or complex.
Question 3: Which behavioral pattern defines a one-to-many dependency so that when one object changes state, all its dependents are notified automatically?
- Mediator
- Chain of Responsibility
- Observer (Correct answer)
- Event Sourcing
Correct answer: Observer
The Observer pattern (also called Publish-Subscribe) automatically propagates changes from a subject to all registered observers.
Question 4: What problem does the Flyweight pattern solve?
- Reducing coupling between subsystems
- Controlling access to an object
- Supporting a large number of fine-grained objects efficiently by sharing state (Correct answer)
- Providing multiple constructors for a complex object
Correct answer: Supporting a large number of fine-grained objects efficiently by sharing state
Flyweight reduces memory usage by sharing intrinsic (immutable) state across many similar objects, storing extrinsic state outside.
Question 5: The Chain of Responsibility pattern passes a request along a chain of handlers. What happens when no handler in the chain processes the request?
- An exception is always thrown
- The request reaches the end of the chain unhandled — behavior depends on implementation (Correct answer)
- The request loops back to the first handler
- The pattern is considered broken
Correct answer: The request reaches the end of the chain unhandled — behavior depends on implementation
By default, an unhandled request reaches the end of the chain; whether to throw, log, or silently drop it is an implementation decision.
Question 6: Which pattern allows adding new operations to existing object structures without modifying those objects?
- Strategy
- Visitor (Correct answer)
- Decorator
- Command
Correct answer: Visitor
The Visitor pattern lets you define a new operation by creating a visitor class that implements visit methods for each concrete element type.
Question 7: How does the Facade pattern differ from the Adapter pattern?
- Facade wraps multiple subsystems to simplify an interface; Adapter wraps one class to match a different interface (Correct answer)
- Facade is behavioral; Adapter is creational
- Facade changes behavior; Adapter only changes structure
- They are functionally identical
Correct answer: Facade wraps multiple subsystems to simplify an interface; Adapter wraps one class to match a different interface
Facade simplifies a complex subsystem with a unified interface, while Adapter translates one specific interface to match what a client expects.
The Bridge pattern separates an abstraction from its implementation so that the two can vary independently.
What is the core structural mechanism?