Python: Builder Design pattern
The builder Design pattern is a creational design pattern, which allows the construction of complex objects step by step. The aim is to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.”
The Builder Pattern focuses on answering the following questions.
- How can a class create different representations of a complex object?
- How can a class that includes creating a complex object be simplified?
The Builder
and Factory patterns
are very similar in the fact they are both instantiate
new objects
at runtime. But they are different as the Builder
controls the object creation process however Factory or Abstract Factory design patterns take over the responsibility of creating objects.
So we can useBuilder design patterns
when the process of creating the object is more complex, so rather than the Factory returning a new instance of Object1, it calls the builders' director constructor method Object1.construct()
that goes through a more complex construction process involving several steps and then returns the final complex object.
Terminology
In the general implementation of the Builder Pattern, we have 4 actors: