Pattern Design Structural Design Patterns 1 — Questions and Answers
Question 1: The Adapter pattern is used to:
- Make incompatible interfaces work together (Correct answer)
- Add responsibilities to an object dynamically
- Provide a simplified interface to a complex subsystem
- Share common state between many objects
Correct answer: Make incompatible interfaces work together
The Adapter pattern converts the interface of a class into another interface that clients expect, enabling collaboration between incompatible classes.
Question 2: The Decorator pattern differs from inheritance because it:
- Adds behavior dynamically at runtime (Correct answer)
- Creates a new subclass for each feature
- Modifies the original class
- Uses multiple inheritance
Correct answer: Adds behavior dynamically at runtime
Decorator wraps objects at runtime to add new behaviors without altering the original class or creating large inheritance hierarchies.
Question 3: Which structural pattern provides a single simplified interface to a complex subsystem?
- Facade (Correct answer)
- Adapter
- Proxy
- Bridge
Correct answer: Facade
The Facade pattern defines a higher-level interface that makes a subsystem easier to use by hiding its internal complexity.
Question 4: The Composite pattern is most useful for representing:
- Tree-structured hierarchies of objects (Correct answer)
- A simplified view of a complex system
- State-based transitions
- Objects with shared intrinsic state
Correct answer: Tree-structured hierarchies of objects
Composite allows individual objects and compositions of objects to be treated uniformly, making it ideal for tree structures like file systems or UI components.
Question 5: In the Bridge pattern, abstraction and implementation are separated to allow them to:
- Vary independently of each other (Correct answer)
- Share the same interface
- Be instantiated as singletons
- Be composed in a tree structure
Correct answer: Vary independently of each other
Bridge decouples an abstraction from its implementation so that both can be changed independently without affecting each other.
Question 6: The Flyweight pattern reduces memory usage by:
- Sharing common intrinsic state between multiple objects (Correct answer)
- Deferring object creation to subclasses
- Wrapping objects with new behavior
- Providing a surrogate for another object
Correct answer: Sharing common intrinsic state between multiple objects
Flyweight extracts the shared (intrinsic) state into a single shared object, while each instance only stores unique (extrinsic) state.
The Adapter pattern is used to: