CPP Design Patterns & Architecture 3 — Questions and Answers
Question 1: Which pattern is best suited for implementing undo/redo functionality in an application?
- Memento
- State
- Command (Correct answer)
- Observer
Correct answer: Command
The Command pattern encapsulates operations as objects with execute/undo methods, making undo/redo straightforward.
Question 2: In the Flyweight pattern, what is the key distinction between intrinsic and extrinsic state?
- Intrinsic state is mutable; extrinsic state is immutable
- Intrinsic state is shared across instances; extrinsic state is context-specific (Correct answer)
- Intrinsic state is stored outside the flyweight; extrinsic state inside
- Intrinsic state changes per call; extrinsic state is constant
Correct answer: Intrinsic state is shared across instances; extrinsic state is context-specific
Flyweight stores intrinsic (invariant) state internally and receives extrinsic (varying) state from the client at runtime.
Question 3: Which architectural pattern divides an application into Model, View, and Presenter components?
- MVC
- MVVM
- MVP (Correct answer)
- PAC
Correct answer: MVP
MVP (Model-View-Presenter) replaces the Controller with a Presenter that mediates between View and Model with no direct View-Model connection.
Question 4: What is the primary intent of the Null Object pattern?
- Represent missing objects with a no-op implementation to avoid null checks (Correct answer)
- Wrap null pointers with smart pointers
- Throw exceptions instead of returning null
- Log null access attempts at runtime
Correct answer: Represent missing objects with a no-op implementation to avoid null checks
Null Object provides a default do-nothing implementation, eliminating the need for null checks scattered throughout client code.
Question 5: In the Visitor pattern, what is 'double dispatch' and why is it needed?
- Two virtual calls that resolve the concrete types of both the visitor and the element (Correct answer)
- Calling the same method twice for thread safety
- Dispatching to two different visitor implementations simultaneously
- Using two inheritance chains to separate interface from implementation
Correct answer: Two virtual calls that resolve the concrete types of both the visitor and the element
Double dispatch uses two polymorphic calls — one on the element to accept a visitor, and one on the visitor to act on the concrete element type.
Question 6: Which principle does the Dependency Inversion Principle (DIP) state?
- High-level modules should not depend on low-level modules; both should depend on abstractions (Correct answer)
- Dependencies should be initialized before use
- Classes should depend on the most derived type available
- Low-level modules control the flow of high-level modules
Correct answer: High-level modules should not depend on low-level modules; both should depend on abstractions
DIP states that both high-level and low-level modules should depend on abstractions, reducing coupling between layers.
Question 7: Which pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling?
- Observer
- Mediator (Correct answer)
- Facade
- Proxy
Correct answer: Mediator
The Mediator pattern centralizes complex communications between objects, so they interact through the mediator rather than directly.
Which pattern is best suited for implementing undo/redo functionality in an application?