Pattern Design Design Pattern 3 — Questions and Answers
Question 1: How does the Strategy pattern differ from the State pattern even though both change an object's behavior at runtime?
- Strategy changes behavior based on internal state transitions; State uses external configuration
- Strategy encapsulates interchangeable algorithms chosen by the client; State encapsulates behavior tied to internal state transitions (Correct answer)
- Strategy uses inheritance; State uses composition
- Strategy applies only to sorting algorithms; State applies only to UI components
Correct answer: Strategy encapsulates interchangeable algorithms chosen by the client; State encapsulates behavior tied to internal state transitions
Strategy lets clients choose among interchangeable algorithms, while State manages behavior changes driven by the object's own internal state machine.
Question 2: What is the primary intent of the Composite pattern?
- Reduce the number of objects by sharing common state
- Treat individual objects and compositions of objects uniformly through a common interface (Correct answer)
- Provide a surrogate to control access to another object
- Define a family of algorithms and make them interchangeable
Correct answer: Treat individual objects and compositions of objects uniformly through a common interface
Composite lets clients treat single leaf objects and composite tree structures identically through a shared component interface.
Question 3: Which behavioral pattern decouples the sender of a request from its receiver by passing the request along a chain of handlers?
- Mediator
- Observer
- Chain of Responsibility (Correct answer)
- Command
Correct answer: Chain of Responsibility
Chain of Responsibility passes a request along a chain of potential handlers, each deciding whether to handle it or forward it.
Question 4: In the Proxy pattern, which variant is used to defer the creation of an expensive object until it is actually needed?
- Protection Proxy
- Remote Proxy
- Virtual Proxy (Correct answer)
- Logging Proxy
Correct answer: Virtual Proxy
A Virtual Proxy delays creation and initialization of an expensive object until a client first requests it.
Question 5: A team wants to log every method call on a database connection without modifying the connection class. Which pattern is most appropriate?
- Decorator (Correct answer)
- Flyweight
- Builder
- Memento
Correct answer: Decorator
Decorator wraps the existing object and adds logging behavior transparently, without altering the original class.
Question 6: What role does the Mediator pattern play in reducing coupling among components?
- It merges multiple components into a single class
- It centralizes communication so components interact through the mediator rather than directly with each other (Correct answer)
- It caches repeated messages between components
- It converts component interfaces so they are compatible
Correct answer: It centralizes communication so components interact through the mediator rather than directly with each other
Mediator centralizes interactions among components, preventing them from referencing each other and reducing coupling.
Question 7: Which creational pattern constructs a complex object step by step and allows the same construction process to produce different representations?
- Abstract Factory
- Factory Method
- Prototype
- Builder (Correct answer)
Correct answer: Builder
Builder separates the construction of a complex object from its representation, using a step-by-step director process that can yield different products.
How does the Strategy pattern differ from the State pattern even though both change an object's behavior at runtime?