Junior — Middle
Can you name the main creational design patterns?
sobes.tech AI
Answer from AI
The main creational design patterns are patterns that help create objects by hiding their creation details and simplifying the process. They include:
- Singleton — ensures only one instance of a class exists and provides a global access point.
- Factory Method — defines an interface for creating an object but allows subclasses to change the type of objects created.
- Abstract Factory — provides an interface for creating families of related objects without specifying their concrete classes.
- Builder — separates the construction of a complex object from its representation, allowing different representations.
- Prototype — creates new objects by copying existing ones.
Example of Singleton in Swift:
class Singleton {
static let shared = Singleton()
private init() { }
func doSomething() {
print("Singleton works")
}
}
Singleton.shared.doSomething()