Software Engineering Software Design Patterns 2 — Questions and Answers
Question 1: Which pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable?
- Command
- Strategy (Correct answer)
- Template Method
- State
Correct answer: Strategy
The Strategy pattern lets the algorithm vary independently from the clients that use it.
Question 2: The Composite pattern is most useful when you need to treat individual objects and compositions of objects:
- Differently
- Uniformly (Correct answer)
- Sequentially
- Randomly
Correct answer: Uniformly
The Composite pattern lets clients treat individual objects and compositions uniformly through a common interface.
Question 3: Which pattern separates the construction of a complex object from its representation?
- Prototype
- Abstract Factory
- Builder (Correct answer)
- Flyweight
Correct answer: Builder
The Builder pattern constructs a complex object step by step and allows the same construction process to create different representations.
Question 4: Which behavioral pattern encapsulates a request as an object, allowing parameterization and queuing?
- Command (Correct answer)
- Mediator
- Memento
- Visitor
Correct answer: Command
The Command pattern encapsulates actions as objects, enabling undo/redo, logging, and queued requests.
Question 5: What is the primary intent of the Proxy design pattern?
- Add new behaviors to objects
- Control access to another object (Correct answer)
- Notify observers of state changes
- Define a skeleton algorithm
Correct answer: Control access to another object
The Proxy pattern provides a surrogate or placeholder for another object to control access to it.
Question 6: Which pattern allows an object to alter its behavior when its internal state changes, appearing to change its class?
- Strategy
- State (Correct answer)
- Interpreter
- Iterator
Correct answer: State
The State pattern encapsulates state-dependent behavior in separate state objects and delegates behavior to the current state.
Which pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable?