Builder by

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Motivation

Complex as in made up of multiple parts, such as complex buildings or complex numbers.

Builders - Has all the code to create and assemble a particular kind of product. It defines what the parts are, knows how to create them and attach to other parts.
Directors - Each director knows how to build a certain type of complex object out of a set of parts. The object that is created depends on which type of builder it is working with.

Think of builders as factories for parts. You could have builders creating tiles for the level. An ice world builder would have snowy and frozen tiles, while a sea world builder could return underwater tiles. By swapping out the builder, the small parts making up the complex object change.

A director is the one with the blueprint. For example, one director class could be designed to create a simple square room, while another a narrow tunnel connecting two different rooms. The choice of director decides the structural outcome, while the builder provides all the individual pieces. By swapping out the director, the way that the different small parts are put together changes

 

Structure

 

  • The client creates a building director and configures it with a concrete builder.
  • The director instructs the concrete builder how to construct a complex product step by step with methods defined by the builder interface.
  • The client gets the final product from the concrete builder.
  • The builder interface does not define a GetResult method, so that the director would not be aware of the type of products being produced.

Example Uses

Use the Builder pattern when:

  • The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
  • The construction process must allow different representations for the object that's constructed.

Applying to games

  • It has many uses for world building. By having multiple builders work with multiple directors and vice versa, we are able to get a lot of variation out of our code.
  • It is possible to create in game crafting systems, by having the director behavior depend on player input. 
;