Working with Builder Software Design Pattern

Hello friends,

Hope you are doing great. Today Lets go through another software design pattern called Builder.

Before talking about its implementation let’s begin with defining it.

As per GOF, Builder pattern is defined as following.

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

Well, let’s understand what they mean. As the name suggests, builder pattern is used to build or construct objects by parts or step-by-step. The complex logic of object construction is separated from client ,and client gets the required object and information just by passing a set of details. For example, to build a car, we first need to build the body, engine, and seats and so on. Builder pattern follows the same approach.
Builder pattern has four sub-components as in the following.

  • Product
  • Builder
  • Concrete Builder
  • Director

Now, let’s see how to implement this pattern by taking the same car example.

Firstly, we need to have few enums to outline the parts as in the following.

  • public enum BodyType  
  • {  
  •     HatchBack,  
  •     Sedan,  
  •     SUV  
  • }  
  •   
  • public enum EngineType  
  • {  
  •     Petrol,  
  •     Diesal,  
  •     Hybrid  
  • }  
  •   
  • public enum SeatType  
  • {  
  •     Regular,  
  •     SemiSleeper,  
  •     Sleeper  
  • }  

So, the parts are ready. Now, let’s build the product; i.e., Car. Car class need to have attributes required to build the car. 

  • class Car  
  • {  
  •     string carName;  
  •     public Car(stringcarName)  
  •     {  
  •         this.carName = carName;  
  •     }  
  •     public string CarName  
  •     {  
  •         get   
  •         {  
  •             return carName;  
  •         }  
  •     }  
  •     public BodyType CarBodyType  
  •     {  
  •         get;  
  •         set;  
  •     }  
  •     public EngineType CarEngineType  
  •     {  
  •         get;  
  •         set;  
  •     }  
  •     public SeatType CarSeatType  
  •     {  
  •         get;  
  •         set;  
  •     }  
  •   
  •     public string GetDetails()   
  •     {  
  •         return string.Format(“Name: {0}\nBodyType: {1}\nEngineType: {2}\nSeatType: {3}”, CarName, CarBodyType, CarEngineType, CarSeatType);  
  •     }  
  • }  

Now we are done with building Product (i.e. car), now let’s create the Builder which should have methods to build various parts of the Product. 

  • interface ICarBuilder  
  • {  
  •     void BuildBody();  
  •     void BuilEngine();  
  •     void BuilSeats();  
  •     Car MyCar  
  •     {  
  •         get;  
  •     }  
  • }  

We have created Builder as well. What next?

Next will be the Concrete Builder, but why?

Well, concrete builder is required to build a variety of cars and to handle customization passed from client. For example, if we want to build Maruti and Skoda cars, we need to have two concrete builders such as MarutiCarBuilder and SkodaCarBuilder as in the following:

  • class MarutiCarBuilder: ICarBuilder   
  • {  
  •     Car car = null;  
  •     public MarutiCarBuilder()  
  •     {  
  •         car = new Car(“Maruti”);  
  •     }  
  •   
  •     public void BuildBody()  
  •     {  
  •         car.CarBodyType = BodyType.HatchBack;  
  •     }  
  •   
  •     public void BuildEngine()  
  •     {  
  •         car.CarEngineType = EngineType.Petrol;  
  •     }  
  •   
  •     public void BuildSeats()  
  •     {  
  •         car.CarSeatType = SeatType.Regular;  
  •     }  
  •   
  •     public CarMyCar   
  •     {  
  •         get  
  •         {  
  •             return car;  
  •         }  
  •     }  
  • }  
  •   
  • class SkodaCarBuilder: ICarBuilder  
  • {  
  •     Car car = null;  
  •     public SkodaCarBuilder()  
  •     {  
  •         car = new Car(“Skoda”);  
  •     }  
  •   
  •     public void BuildBody()   
  •     {  
  •         car.CarBodyType = BodyType.Sedan;  
  •     }  
  •   
  •     public void BuilEngine()   
  •     {  
  •         car.CarEngineType = EngineType.Diesal;  
  •     }  
  •   
  •     public void BuilSeats()   
  •     {  
  •         car.CarSeatType = SeatType.SemiSleeper;  
  •     }  
  •   
  •     public Car MyCar  
  •     {  
  •         get  
  •         {  
  •             return car;  
  •         }  
  •     }  
  • }  

I hope you have realized the beauty and power of Builder pattern that, for any future car, we need to just have one builder created for that car.

So by now we have created three sub-components of Builder pattern i.e. Product, Builder, and Concrete Builder. Let’s create the remaining one, that is Director, which will have a method to build the Product by taking the appropriate builder object.

  • class CarManufacturer  
  • {  
  •     public void BuildCar(ICarBuildercarBuilder)   
  •     {  
  •         carBuilder.BuildBody();  
  •         carBuilder.BuilEngine();  
  •         carBuilder.BuilSeats();  
  •     }  
  • }  

We are done with the setup now. Let’s see how client uses this.

  • static void Main()  
  • {  
  •         Console.Title = “Builder pattern demo”;  
  •         CarManufacturercarMfg = newCarManufacturer();  
  •         ICarBuildercarBuilder = null;  
  •   
  •         ////Build Maruti Car  
  •         carBuilder = newMarutiCarBuilder();  
  •         carMfg.BuildCar(carBuilder);  
  •         Console.WriteLine(“Built Car Specs are:\n\n{0}”, carBuilder.MyCar.GetDetails());  
  •   
  •         ////Build Skoda car  
  •         carBuilder = newSkodaCarBuilder();  
  •         carMfg.BuildCar(carBuilder);  
  •         Console.WriteLine(“Built Car Specs are:\n\n{0}”, carBuilder.MyCar.GetDetails());  
  • }  

Output

Output

I hope you have liked the article. I look forward to your comments/suggestions.

Leave a Reply

Your email address will not be published. Required fields are marked *