Creational 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 in added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
There are several creational patterns, including the following
- Abstract factory pattern: This pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. For example, an abstract factory class for creating desktop and mobile widgets might have methods for creating buttons, text boxes, and other common UI elements, and each method would return a widget that is appropriate for the current platform (desktop or mobile).
- Builder pattern: This pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations. For example, a builder class for creating XML documents might have methods for adding elements, attributes, and other content to the document, and a final “build” method that returns the completed XML document.
- Prototype pattern: This pattern allows for the creation of new objects by copying existing objects. For example, a prototype class for creating copies of objects might have a “clone” method that creates a new object with the same properties as the original object.
- Singleton pattern: This pattern ensures that a class has only one instance, and provides a global point of access to it. For example, a singleton class for managing a database connection might have a “getInstance” method that returns the current instance of the class, creating a new instance if one does not already exist.