Acreational pattern is asoftware design pattern for creatingobjects in a manner suitable to a particular situation. As object creation that is otherwise available (i.e. via theprogramming language) can sometimes result in design limitations, a custom mechanism for creation can provide for better design. A creational pattern aims to separate a system from how its objects are created, composed, and represented. They increase the system's flexibility in terms of the what, who, how, and when of object creation.[1]
A creational pattern encapsulates two main aspects. One is encapsulating knowledge about which concreteclasses the system uses. Another is hiding howinstances of these concrete classes are created and combined.[2]
Creational design patterns are categorized into object-creational patterns and class-creational patterns. Object-creational patterns defer part of its object creation to another object, while class-creational patterns defer its object creation to subclasses.[3]
As modern software engineering depends more onobject composition than class inheritance, emphasis shifts away from hard-coding behaviors toward defining a smaller set of basic behaviors that can be composed into more complex ones.[4] Hard-coding behaviors are inflexible because they require overriding or re-implementing the whole thing in order to change parts of the design. Additionally, hard-coding does not promote reuse and makes it difficult to keep track of errors. For these reasons, creational patterns are more useful than hard-coding behaviors. Creational patterns make design become more flexible. They provide different ways to remove explicit references in the concrete classes from the code that needs to instantiate them.[5] In other words, they create independency for objects and classes.
Consider applying creational patterns when:
A system should be independent of how its objects and products are created.
A set of related objects is designed to be used together.
Hiding the implementations of a class library or product, revealing only their interfaces.
Constructing different representation of independent complex objects.
A class wants its subclass to implement the object it creates.
The class instantiations are specified at run-time.
There must be a single instance and client can access this instance at all times.
Instance should be extensible without being modified.
Below is a simple class diagram that most creational patterns have in common. Note that different creational patterns require additional and different participated classes.