It separates the construction of complex object from its representation, so that number of different representations can be build based on the requirement. It is similar to factory pattern, but it does not return simple descendant of base class. The factory is concerned with what is made and the builder with how it is made. Factory simply returns an instance of related class of base type, but builder returns a composition of various classes depending on the specified state.
- Editor Builder: Suppose you are creating a UI for workflow where a number of different types of nodes can exist. We need different editors to edit these nodes, which can be very complex in structure. Here we can use builder which can build the desired editor. Even editors can vary in structure based on current state of system.
When to Use
- When we have a complex UI element to build whose structure and composition may vary depending on the system state.
- When we want to isolate and abstract the complex UI building mechanism from rest of the system.
- A Builder helps you vary the internal presentation of the product it builds.
- It hides the detail of how the product is assembled.
- Builder build the final product step by step based on data given, so we have more control over the final product.