This pattern works by creating an abstract plan describing the steps for creating a complex object, and then carry out the same steps on different implementations, as long as each implementation implements all the steps.
When the application needs to create complex objects of different types, but all of them are created with the same steps.
When a client needs to create a complex object which is composed of many components which are added to the object as steps, you may decide on building the object right in the client. It may not be the best thing to do, because now the creation steps and the representation of the object are coupled together. After adding another type of complex object which can be built from the same data that the client holds, you end up with a lot of conditionals in your client. Also you end up repeating the same building logic if a different client needs to create the same type of object.
This class declares the interfaces to create parts of a product object
This class implements the interfaces to build one type of product, keeps track of the product which is created step-by-step, and provides a method to get the built product. You'll need as many ConcreteBuilders as the number of object types you need to create, where each type of object has a different internal representation.
This class uses the AbstractBuilder's interfaces to create a product in multiple steps. The Director only knows the steps to create an object. What goes into the object at each step is unknown to it.
A product represents the complex object under construction.
Asks the Director to create an object using a ConcreteBuilder.
- The Director needs to know the steps for creating a complex object. And all complex object types have to implement the steps.
- The base Builder class need not be an abstract class which raises an NotImlementedError, instead, it may choose to define empty methods. This way, the Concrete Builders do not need to implement those steps that have no effect on the internal representation of that type of complex object.
TODO - Add how other patterns are related.