Middle
What are creational design patterns?
sobes.tech AI
Answer from AI
Creational design patterns are templates that abstract the instantiation process, making the system independent of how and when objects are created.
Main creational patterns:
- Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder: Separates the construction of a complex object from its representation so that the same construction process can create different representations.
- Factory Method: Defines an interface for creating an object but allows subclasses to decide which class to instantiate. The factory method lets a class delegate instantiation to subclasses.
- Prototype: Specifies the kinds of objects to create using a prototypical instance and creates new objects by copying this prototype.
- Singleton: Ensures that a class has only one instance and provides a global point of access to it.
Examples of usage:
- Abstract Factory can be used to create UI element sets for different operating systems (e.g., iOS and Android).
- Builder is useful when creating complex objects with many configuration options, such as URLRequest or network configuration.
- Factory Method is often used in frameworks to create views or controllers based on their string identifiers.
- Prototype allows efficient creation of new objects based on existing ones, such as cloning instances.
- Singleton is used for managers like
URLSession.sharedorUserDefaults.standard.
These patterns increase flexibility, reusability, and testability of code, hiding the details of object creation.