Pattern Design Ultimate Design Pattern 5 — Questions and Answers
Question 1: The State pattern allows an object to alter its behavior when its internal state changes. How is this typically implemented?
- Using large switch statements in a single class
- Delegating behavior to a State object that changes at runtime (Correct answer)
- Using reflection to swap method implementations
- Inheriting from multiple state base classes
Correct answer: Delegating behavior to a State object that changes at runtime
The State pattern replaces conditional logic with state objects; the context delegates behavior to the current state object, which can be replaced.
Question 2: In the Observer pattern, what is the risk of having observers that maintain strong references to the subject?
- Deadlocks in multithreaded environments
- Memory leaks if observers are not properly deregistered (Correct answer)
- Stack overflows from recursive notifications
- Thread starvation
Correct answer: Memory leaks if observers are not properly deregistered
If observers hold strong references to the subject and are never removed, they prevent garbage collection, causing memory leaks — especially in long-lived subjects.
Question 3: Which anti-pattern does an overused Singleton often lead to?
- The God Object anti-pattern and hidden global state that makes testing difficult (Correct answer)
- The Adapter anti-pattern
- Excessive subclassing
- Premature optimization
Correct answer: The God Object anti-pattern and hidden global state that makes testing difficult
Singletons introduce hidden global state and tight coupling, making unit testing and parallel execution harder.
Question 4: When would you choose the Prototype pattern over a regular constructor call?
- When you want to enforce a single instance
- When object creation is costly and you can clone a preconfigured instance cheaply (Correct answer)
- When you need lazy initialization
- When the class has no public constructor
Correct answer: When object creation is costly and you can clone a preconfigured instance cheaply
Prototype is ideal when constructing an object from scratch is expensive — cloning a ready-made prototype is faster and simpler.
Question 5: In a multi-threaded environment, which technique makes a Singleton implementation thread-safe without synchronizing every access?
- Eager initialization (creating the instance at class load time) (Correct answer)
- Synchronized getInstance() on every call
- Using a volatile keyword only
- Making the constructor public
Correct answer: Eager initialization (creating the instance at class load time)
Eager initialization creates the Singleton when the class is loaded, leveraging the JVM's class-loading mechanism which is inherently thread-safe.
Question 6: The Decorator pattern and the Proxy pattern are structurally similar. What is their key intent difference?
- Proxy adds new responsibilities; Decorator controls access
- Decorator enriches behavior transparently; Proxy controls or restricts access to the wrapped object (Correct answer)
- They have the same intent — only naming differs
- Decorator is structural; Proxy is behavioral
Correct answer: Decorator enriches behavior transparently; Proxy controls or restricts access to the wrapped object
Decorator focuses on dynamically adding functionality, while Proxy focuses on controlling access (lazy loading, security, caching) to the real object.
Question 7: Which design pattern is most directly applied when implementing an undo/redo system in a text editor?
- Observer
- Memento
- Command
- Both Command and Memento together (Correct answer)
Correct answer: Both Command and Memento together
Command encapsulates each editing action as an object (enabling undo via an inverse execute), while Memento stores snapshots of state to restore — both are typically combined.
The State pattern allows an object to alter its behavior when its internal state changes.
How is this typically implemented?