Understanding and Implementing Builder Pattern in C#

This article talks about the Builder Design pattern, When can this pattern be used and when should it be implemented. We will then see a small rudimentary implementation of Builder pattern in C#.

Background

When we have an application that need to create an object which has to be constructed using many different objects, we find our client code cluttered with the details of the various Part objects that needs to be assembled together to create the resulting object.

To illustrate on above point let us take an example of mobile phone manufacturing system. Lets assume that we have a system installed at one of the mobile phone vendors. Now the manufacturer may decide to create a phone based on parameters like Touchscreen, Operating System, Battery and Stylus. Now if we have the objects for all these parts then creation of product with any combination of above parts would lead to a very complex and unmanageable code in the client application i.e. the module that will decide what kind of phone needs to be built.

Builder pattern is meant to solve such problems. GoF defines Builder pattern as:

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

What this means is that we will have to design the system in such a way that the client application will simply specify the parameters that should be used to create the complex object and the builder will take care of building the complex object. Let us visualize the class diagram of Builder Pattern.

Gof

Now lets see what each class in the above class diagram is meant for

  • ConcreteBuilder: Concrete classes that will create the complex Product. this will keep track of what Product it has created i.e. assembled what parts and this will be used by the client to get the Product object.
  • Builder: This is the interface for creating the actual products
  • Director: This is the Client code that will specify the parts needs to be put tegether to to create the actual concrete Product.
  • Product: This is the object that will be created by assembling many parts.

Using the code

Let us now follow the same lines as defined in the builder pattern and try to implement a basic builder pattern by solving the similar mobile phone manufacturer problem that we discussed earlier.

Let us start by having the mechanism to specify the Parts in place. Let us simple define some enums for each part so that we can create a Product by assembling the various part types

Now let us look the the Productclass. We need to have a Productthat can be created by assembling there parts so lets have a class called MobilePhonewhich will be the Productclass for us.

Now since we have the Productclass ready with us lets work on creating the Builder. The Buildershould provide the functions for creating each of the parts for any phone. So let us create an interface for Builderas IPhoneBuilderand look at it.

Now we have the Builderinterface ready, the next thing would be to have the ConcreteBuilderobjects in place. Let us assume that the manufacturer is planning for an Android phone and one Windows Phone so we will be needing two ConcreteBuilderfor these phones i.e. AndroidPhoneBuilderand WindowsPhoneBuilder. Inside these builders we can specify the type of parts we want to use for each phone.

And finally lets have the Directorclass. Lets create a Directorclass that will have the Construct method accepting an IPhoneBuilderand then calling the respective functions of the ConcreteBuilders internally.

Now we have wrapped around the functionality of building complex Productsin form of Builder Design Pattern. Now when we look at the client code we can see how clean it is to create any product.

Now if we need to create more Productsonly a ConcreteBuilderwould be needed and rest all the codebase will remain the same. The client code can also create complex Products easily with this pattern in place. Lets look at the output of our program.

ScreenShot

Before wrapping up let is look at the class diagram of our application and compare it with the class diagram of Builder Pattern.

Mydiagram

Points of interest

In this article, I tried to describe the Builder pattern, When is it needed and provided a rudimentary implementation of Builder Pattern in C#.

Download sample code for this article: BuilderTest