Pattern Design Design Pattern 4 — Questions and Answers
Question 1: In which scenario is the Prototype pattern more appropriate than the Factory Method pattern?
- When the number of product types is fixed and known at compile time
- When creating objects that are expensive to initialize and many similar copies are needed (Correct answer)
- When the product must be assembled from multiple parts
- When a single global instance is required
Correct answer: When creating objects that are expensive to initialize and many similar copies are needed
Prototype is ideal when creating a new object from scratch is costly and cloning an existing configured instance is cheaper.
Question 2: What problem does the Memento pattern solve?
- It prevents an object from being modified by unauthorized clients
- It captures and externalizes an object's internal state so it can be restored later without violating encapsulation (Correct answer)
- It notifies subscribers whenever an object's state changes
- It defers responsibility for state management to a parent class
Correct answer: It captures and externalizes an object's internal state so it can be restored later without violating encapsulation
Memento captures an object's state in a separate memento object so the original can be restored to that state without exposing private fields.
Question 3: Which structural pattern is best suited when you want to vary both the abstraction and its implementation independently?
- Composite
- Bridge (Correct answer)
- Adapter
- Decorator
Correct answer: Bridge
Bridge decouples an abstraction from its implementation so both can be extended independently through separate inheritance hierarchies.
Question 4: How does the Command pattern enable undo functionality?
- Commands store a reference to the receiver's state before execution and restore it on undo
- Each Command object implements an undo() method that reverses the action performed by execute() (Correct answer)
- The invoker replays all prior commands in reverse order
- Commands broadcast a rollback event to all observers
Correct answer: Each Command object implements an undo() method that reverses the action performed by execute()
Each Command encapsulates the action in execute() and the reverse action in undo(), making undo straightforward to implement.
Question 5: A report generator must produce reports in PDF, HTML, and CSV without the report logic knowing the output format. Which pattern handles this best?
- Template Method with format-specific subclasses (Correct answer)
- Observer with format listeners
- Singleton with a global format setting
- Flyweight sharing format objects
Correct answer: Template Method with format-specific subclasses
Template Method defines the report generation algorithm in the base class and delegates format-specific rendering steps to subclasses for PDF, HTML, and CSV.
Question 6: What is the key difference between the Facade pattern and the Mediator pattern?
- Facade simplifies access to a subsystem from outside; Mediator manages interactions among components within a system (Correct answer)
- Facade adds new behavior; Mediator restricts behavior
- Facade applies only to legacy systems; Mediator applies only to UI
- Facade requires inheritance; Mediator requires composition
Correct answer: Facade simplifies access to a subsystem from outside; Mediator manages interactions among components within a system
Facade provides a simplified external interface to a subsystem, while Mediator coordinates communication among objects inside a system.
Question 7: Which GoF pattern category does the Iterator pattern belong to?
- Creational
- Structural
- Behavioral (Correct answer)
- Concurrency
Correct answer: Behavioral
Iterator is a behavioral pattern because it defines how objects communicate by providing a way to sequentially access collection elements.
In which scenario is the Prototype pattern more appropriate than the Factory Method pattern?