
Creational design patterns focus on abstracting the instantiation process. They aid in making a system independent of how its objects are created, composed, and represented. Creational design patterns can be categorized into two groups, class creational patterns, which use inheritance, and object creational patterns, which delegate instantiation to another object. In this post, we’ll lightly discuss 5 creational patterns noted by The Gang of Four.
Abstract Factory
Let’s start with the abstract factory. The abstract factory pattern allows you to create multiple objects that follow a general pattern. It’s best used when the client doesn’t rely on how you create and compose objects or when the system has multiple families of objects that are designed to be used together.
The abstract factory is an object used to create families of objects. While the factory method removes the specifics about the way that objects are created, the abstract factory removes the specifics about how those factories are created. Because of this, the abstract factory is also commonly known as the “Factory factory”.
This design pattern finds its strength in creating pre-defined objects, but creating new types of objects involves altering the abstract factory class itself as well as all of its subclasses. The general breakdown of this pattern would go something like this: the abstract factory would create a class factory, which would create a specific class, which would be used to create an interface.
Builder
Builders separate the construction of complex objects from their representations. This allows builders to be used to create different representations of a generally similar object. This pattern is useful for creating a complex object step by step. For example, if you wanted to create multiple car objects you could create a builder to handle that process for you. Each representation of the car may be different, but the general makeup is the same.
Builders can also contain a director class, which is used to define the order in which to run the building steps. The director is also useful for hiding the details of the object construction from the client code. The client can connect a builder to a director, tell the director what steps to…