Builder Design Pattern

The purpose of Builder Design Pattern is to separate the construction of complex object from its representation so that the same construction process can create different representation. It is usually use in building complex objects by using a step by step approach.

The best practical example I could think is Language Translator. Where there are series of steps involved before language can be translated (such as parsing of text, lexical analysis, grammar, etc) .  But I won’t use this to explain this pattern.

UML Diagram

Figure 1: UML Class Diagram for Builder Design Pattern

Participants
Class/Interface Description
Director Uses the ConcreteBuilder. Knows how build. Client code calls directly
Builder Interface that defines the steps in creating product.
ConcreteBuilder Provides an implementation interface defined by the builder.  This class can be multiple depending on how many builders will be created. This is where the logic is found.
Product This is what is being build. Its not a different type, but holds different data. It defines that data to be used by the builder pattern.

Sample Code and Implementation

Let's reinforce this with a real world example using House Builder that involves the participants. Let's get started.

Download Source Code


The Project Structure



UML Class Diagram


Mapping
Design Pattern House Builder Application
Director HouseCreator
Builder IHouseBuilder
ConcreteBuilder CountryHomeBuilders, DreamBuilers
Product House

The Codes

The Interface
IHouseBuilder.cs
The Product
House.cs
The ConcreteBuilders
CountryHomeBuilders.cs
DreamBuilders.cs
The Director
HouseCreator.cs

The Client
Program.cs

The Output


Hope this article helpful. Please drop some feedbacks!