Creational Patterns Summary
In this article, there will be a summary of what has been defined in the previous articles about creational patterns and some final considerations.
Creational design patterns establish the precise object of a class given a problem. This generally occurs when you have different instances of different classes. These patterns give flexibility about what is created, who creates it, how it is created, and when.
Here is a brief definition of each creational design pattern:
- Abstract Factory – instantiates multiple class families.
- Factory Method – instantiates multiple derived classes.
- Builder: separates the construction of an object from its representation.
- Prototype: a previously initialized instance is used to be cloned.
- Singleton – A class of which only one instance can exist.
Some considerations and comparisons between these patterns are:
- Abstract Factory classes are often implemented with the Factory Method, but they can also be implemented with Prototype.
- A Concrete Factory is usually a Singleton.
- Abstract Factory is a similar pattern to Builder since complex objects can also be built with this pattern. The most significant difference is that Builder concentrates on building a complex object step by step, while Abstract Factory emphasizes families of objects in a product, whether they are simple or complex. Builder returns the product in the final step and in Abstract Factory the product is returned immediately.
- Prototypes do not require a subclass creator. These often require an operation that initializes them in the product class. Factory Method does not require such an operation.
- Prototype and Abstract Factory compete in a certain way, although they can also be used together. In Abstract Factory you could save a set of prototypes to clone objects.
- Patterns like Abstract Factory, Builder, and Prototype can be implemented using the Singleton pattern.
- There are two ways to parameterize a system through the types of objects that are created in it.
- One way is through subclasses that create objects, as is done in the Factory Method. The downside to this approach is that it may be necessary to create new classes just to change the class of a product.
- The other way is to parameterize a system based on the composition of the objects:
- In Abstract Factory you have a Factory object producing objects of various kinds.
- In Builder you have a Factory object building a complex product, incrementally, using a complex protocol.
- Prototype has a Factory object that builds a product, by cloning a prototype.
- Factory Method makes a design less complicated. This method is often used as the default way to create objects, but it is not necessary in cases where the instantiated class does not change. Frequently, an evolution to other creational patterns is made when more flexibility is required.
- Knowing each of these patterns gives you more options when deciding to go for one pattern over the others.