Creational Patterns is the first major category of GoF design patterns, with a core focus on the mechanism of object creation. Its goal is to separate object creation from its use, making the system more in line with the principle of “programming to interfaces”
Creating objects directly using new(which in C++ allocates memory on the stack or heap) introduces two problems:
new ClassA() hardcodes the implementation, violating the “OSP” when flexibility is needed(e.g., dynamically switching databases based on configuration).new operations lead to code duplication and are prone to errors.The core idea of creational patterns is to encapsulate, delegate, or manage the new operation(or the instantiation of a concrete class).
The GoF defined 5 creational patterns, which can be divided into two categories based on their encapsulation and management granularity:
new to the subclass. You are calling a method that returns an abstract object, regardless of its concrete implementation.new and initialization steps into a series of assembly steps, ultimately generating the object all at once.