Pattern Design Creational Design Patterns 1 — Questions and Answers
Question 1: Which creational pattern ensures only one instance of a class exists throughout the application lifecycle?
- Singleton (Correct answer)
- Factory Method
- Prototype
- Builder
Correct answer: Singleton
The Singleton pattern restricts instantiation of a class to a single object and provides a global access point to it.
Question 2: The Factory Method pattern delegates object creation to:
- Subclasses (Correct answer)
- Abstract classes only
- The client code directly
- Static utility methods
Correct answer: Subclasses
Factory Method defines an interface for creating objects but lets subclasses decide which class to instantiate.
Question 3: Which pattern creates objects by copying an existing object?
- Prototype (Correct answer)
- Singleton
- Builder
- Abstract Factory
Correct answer: Prototype
The Prototype pattern creates new objects by cloning an existing instance, avoiding the cost of creating objects from scratch.
Question 4: The Builder pattern is most useful when:
- An object requires many optional parameters to construct (Correct answer)
- Only one instance is needed
- Objects must be cloned quickly
- Families of related objects are needed
Correct answer: An object requires many optional parameters to construct
The Builder pattern separates the construction of a complex object from its representation, making it ideal for objects with many optional configuration steps.
Question 5: Abstract Factory differs from Factory Method in that it:
- Creates families of related objects (Correct answer)
- Creates a single product type
- Uses inheritance instead of composition
- Requires a concrete creator class
Correct answer: Creates families of related objects
Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Question 6: In the Builder pattern, what role does the Director play?
- Orchestrates the construction steps using the builder interface (Correct answer)
- Creates the final product object
- Defines the blueprint for the product
- Clones the existing product
Correct answer: Orchestrates the construction steps using the builder interface
The Director defines the order in which to call construction steps, allowing reuse of construction routines across different builders.
Which creational pattern ensures only one instance of a class exists throughout the application lifecycle?