Pattern Design Creational Design Patterns 2 — Questions and Answers
Question 1: What problem does the Singleton pattern primarily solve?
- Uncontrolled instantiation of a class (Correct answer)
- Complex object construction
- Object cloning overhead
- Creating product families
Correct answer: Uncontrolled instantiation of a class
Singleton solves the problem of having multiple conflicting instances by ensuring only one instance is created and shared globally.
Question 2: Which creational pattern is best suited for creating UI component libraries that must maintain a consistent theme?
- Abstract Factory (Correct answer)
- Singleton
- Prototype
- Builder
Correct answer: Abstract Factory
Abstract Factory ensures all components (buttons, checkboxes, dialogs) belong to the same family, maintaining visual consistency.
Question 3: Lazy initialization in the Singleton pattern means:
- The instance is created only when first requested (Correct answer)
- The instance is created at application startup
- Multiple instances can exist temporarily
- The constructor is made public
Correct answer: The instance is created only when first requested
Lazy initialization defers the creation of the singleton instance until it is actually needed, saving resources if it is never used.
Question 4: Which creational pattern is commonly used for parsing configuration files into complex domain objects?
- Builder (Correct answer)
- Prototype
- Singleton
- Factory Method
Correct answer: Builder
The Builder pattern is ideal for constructing complex objects like configuration models step-by-step from parsed input.
Question 5: A Prototype registry stores:
- Pre-built prototype objects ready for cloning (Correct answer)
- Factory methods for each class
- Singleton references
- Builder director configurations
Correct answer: Pre-built prototype objects ready for cloning
A Prototype registry (or cache) maintains a set of pre-configured prototype objects that clients can clone on demand.
Question 6: Which pattern violates the Single Responsibility Principle the most if not carefully managed?
- Singleton (Correct answer)
- Factory Method
- Builder
- Prototype
Correct answer: Singleton
Singleton conflates two responsibilities: managing its own instantiation and providing its core business functionality, which can violate SRP.
What problem does the Singleton pattern primarily solve?