Builder Pattern – The Different versions

By | March 21, 2017

Hello, In this article I will discuss about one of the simplest considered pattern after singleton pattern. This is the builder pattern. I will discuss here two variations or versions of the builder pattern with C# examples.

Become an Expert in Programming and Web Development.View all courses!

What is Builder Pattern

As per Gang of four definition “Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

Why Should you use Builder Pattern

As read in the definition, the builder and director will take care of the building of the instance of the class(Product). There can be various representations of the product which will be decided by the builder and no one else.

In this section I will use the director which in turn will use the builder to create the product.

The builder and director helps to get the final product. There can be various builders and directors working together to get the final product as result.

I want to design a system which has a single product named Car. But the car have different variants i.e. Economy, Medium and Luxury. These variants will vary as per the features like Wheels, Upholstery, Color and other properties.

There are some defined steps which we need to perform before getting the end result like first add the wheel, then upholstery, followed by color and so on.

Builder Pattern

Builder Pattern

 

As seen in the above figure the Client only knows about the director and builder class.

The builder interface will have different version which will adhere to the creation process.

As you can see in the above code, the builder has different implementations.

These are Economy, Medium, and Luxury. All the individual classes which implement the ICarBuilder and return the final product.

Now it depends on the client which variant of Car does he need.

In the above code the client needs only the Normal car. If you are the client and want some other variant of the Car, you can use any other variant.

Now suppose if the car company plans to come up with some fourth variant, you can guess how easy it is to extend the above design to get some new variant keeping the construction process same.

Or if we want to change the building process, in that case the change is also very simple.

We can use this version of builder pattern when

  • The object being constructed has different representations.
  • we want to have control over construction of the object.
  • The algorithm for creating parts is different from the parts themselves.

Building Class using Fluent Syntax and Omitting Multiple Constructor Arguments

There is one more utilization of builder pattern. In many cases we may encounter a class needing many constructor arguments and sometimes it becomes highly cumbersome to have too  many parameters for a c# class constructor.

It becomes highly confusing for the users of the class which has too many constructors. The user of the class does not know which constructor argument to pass and which one to skip to get the desired instance of the class.

And it can be highly error prone if we just interchange any of the argument while creating the instance.

As an example lets see the below class code.

While creating an instance of the above class the client has to pass all the constructor arguments. What if by mistake the client interchanges the city and country. The whole functionality will be wrong.

What if a requirement comes that the Person should be initialized with firstName and LastName only. In that case you have to write one more constructor.

To overcome all these problems there is one more version of the builder pattern as shown in the code below.

We can call it as fluent syntax for class creation using builder. The main class has a private constructor so that the instance cannot be created outside of the class. The builder is the nested class which has individual methods for each constructor parameter of the previous example.

Please check the code below.

Now at the client we can call the above code as shown below. You can see that the client is creating two instances of the Person object. One with City and other without city.

Its an exercise to the reader to add more parameters to the Person class in the above code and test it.

As you can see the above code makes it very easy for the user of the class to create the instances of the class without getting confused. And he can explicitly provide the arguments which he wants.

But there is no director present in this version of the builder pattern.

Conclusion:

In this article I have shown you the two versions of the builder pattern in C#. It depends on the kind of scenario where you want to use the pattern. If you want to follow some steps to create an instance of the complex object you can go with the builder pattern with director. If not you can simply use the next version.