This module explores creational patterns
, which represent patterns that are often used in place of direct instantiation with constructors. Creational patterns make the creation process more adaptable and dynamic.
In particular, they can provide a great deal of flexibility about which objects are created, how those objects are created, and how they are initialized.
In this module, you will learn:
- How creational patterns help programmers
- Five well-known creational patterns
- The elements of the Factory Method pattern
- How to incorporate the Factory Method pattern into your course project
In software engineering, 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.
Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created,composed, and represented.
A class creational pattern uses inheritance to vary the class that's instantiated, whereas an object creational pattern
will delegate instantiation to another object.
Creational patterns become important as systems evolve to depend more on object composition than class inheritance.
As that happens, emphasis shifts away from hard-coding a fixed set of behaviors toward defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones.
Thus creating objects with particular behaviors requires more than simply instantiating a class.
There are two recurring themes in these patterns.
- First, they all encapsulate knowledge about which concrete classes the system uses.
- Second, they hide how instances of these classes are created and put together.
The only thing the system at large knows about are the objects and their interfaces as defined by abstract classes.
Consequently, the creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when. They let you configure a system with "product" objects that vary widely in structure and functionality.
Configuration can be static (that is, specified at compile-time) or dynamically. Sometimes creational patterns are competitors. For example, there are cases when either Prototype or Abstract Factory could be used to your advantage.
At other times they are complementary: Builder can use one of the other patterns to implement which components get built.