What is the main purpose of the Singleton pattern in software design?
Think about controlling the number of instances of a class.
The Singleton pattern restricts a class to a single instance and provides a global access point to that instance, ensuring controlled access and resource management.
Which statement best describes the Factory pattern?
Focus on object creation and subclass responsibility.
The Factory pattern defines an interface for creating objects but lets subclasses decide which class to instantiate, promoting loose coupling.
Given a scenario where you need to create different types of complex documents with many optional parts, which creational pattern is most suitable?
Think about building complex objects with many optional parts.
The Builder pattern is ideal for constructing complex objects step by step, especially when the object can have different representations or optional parts.
Which of the following correctly contrasts the Singleton and Factory patterns?
Focus on what each pattern controls or manages.
Singleton ensures only one instance of a class exists, while Factory controls which type of object to create without specifying the exact class.
Consider a Singleton implementation in a multi-threaded environment. What is the main risk if the Singleton is not implemented with thread safety?
Think about what happens when two threads try to create the instance at the same time.
Without thread safety, multiple threads can create separate instances simultaneously, violating the Singleton pattern's guarantee of a single instance.