Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
There are several creational design patterns, including:
- Abstract factory pattern: This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. An abstract factory pattern is often used when a system must be independent of the way the objects it creates are produced. The abstract factory pattern is used to provide a client with a set of related objects, but without specifying their concrete classes. The client uses the abstract factory to create the objects, which are then used to accomplish some task.
- Builder pattern: This pattern separates the construction of a complex object from its representation, so that the same construction process can create different representations. The builder pattern is often used when the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled. The builder pattern is used to create complex objects by specifying the type and content of each object in a step-by-step fashion. The final step creates the object.
- Factory method pattern: This pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. The factory method pattern is used to create objects in a super class, but allow subclasses to alter the type of objects that will be created.
- Object pool pattern: This pattern avoids the expensive acquisition and release of resources by recycling objects that are no longer in use. An object pool is a set of initialized objects that are kept ready to use, rather than allocated and destroyed on demand. The object pool pattern is used to improve performance by reducing the cost of creating and destroying objects.
- Prototype pattern: This pattern specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype. The prototype pattern is used when the creation of an object is costly, or when the object’s initialization is complex. The prototype pattern is used to create new objects by copying existing objects.
- Singleton pattern: This pattern ensures that a class has only one instance, and provides a global point of access to it. Singleton is often used when we need to limit the number of objects created, for example, to conserve system resources. The singleton pattern is used to ensure that a class has only one instance, while providing a global access point to this instance.