Pattern Design Structural Design Patterns 2 — Questions and Answers
Question 1: A Proxy pattern can be used to implement all of the following EXCEPT:
- Converting incompatible interfaces (Correct answer)
- Lazy initialization of heavy objects
- Access control to sensitive resources
- Logging calls to a real object
Correct answer: Converting incompatible interfaces
Converting incompatible interfaces is the role of the Adapter pattern, not Proxy — Proxy keeps the same interface but controls access to the real subject.
Question 2: Which structural pattern is most helpful when you want to add logging, caching, or security checks transparently?
- Proxy (Correct answer)
- Facade
- Flyweight
- Composite
Correct answer: Proxy
The Proxy pattern wraps a real object and intercepts calls to it, making it ideal for cross-cutting concerns like logging or caching.
Question 3: The Class Adapter pattern uses ________ while the Object Adapter uses ________.
- Inheritance; composition (Correct answer)
- Composition; inheritance
- Delegation; aggregation
- Overriding; overloading
Correct answer: Inheritance; composition
A Class Adapter inherits from both the target interface and the adaptee, while an Object Adapter wraps (composes) an instance of the adaptee.
Question 4: In the Composite pattern, what is the key characteristic of leaf nodes?
- They have no children and implement actual behavior (Correct answer)
- They delegate all work to their children
- They act as the entry point to the hierarchy
- They hold shared intrinsic state
Correct answer: They have no children and implement actual behavior
Leaf nodes are the basic elements of a tree with no children; they perform actual work rather than delegating to sub-components.
Question 5: When should you prefer the Bridge pattern over simple inheritance?
- When both abstraction and implementation need to be extensible independently (Correct answer)
- When objects need to be cloned efficiently
- When a single global instance is required
- When object creation is delegated to subclasses
Correct answer: When both abstraction and implementation need to be extensible independently
Bridge avoids the exponential growth of subclasses that occurs when both the abstraction and implementation have multiple dimensions of variation.
Question 6: Flyweight's extrinsic state is:
- Unique per object instance and passed by the client (Correct answer)
- Shared among all flyweight instances
- Stored inside the flyweight object
- Immutable and pre-configured at startup
Correct answer: Unique per object instance and passed by the client
Extrinsic state varies per context and is supplied by the client at runtime, so it is NOT stored inside the flyweight itself.
A Proxy pattern can be used to implement all of the following EXCEPT: