Design Patterns Simplified - Part 7 (Builder)

I am here to continue the discussion around Design Patterns. Today we will discuss another creational design pattern called Builder.


In case, you have not had a look at our previous article, go through the following link:
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.
  1. public enum BodyType  
  2. {  
  3.     HatchBack,  
  4.     Sedan,  
  5.     SUV  
  6. }  
  7.   
  8. public enum EngineType  
  9. {  
  10.     Petrol,  
  11.     Diesal,  
  12.     Hybrid  
  13. }  
  14.   
  15. public enum SeatType  
  16. {  
  17.     Regular,  
  18.     SemiSleeper,  
  19.     Sleeper  
  20. }  
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.
  1. class Car  
  2. {  
  3.     string carName;  
  4.     public Car(stringcarName)  
  5.     {  
  6.         this.carName = carName;  
  7.     }  
  8.     public string CarName  
  9.     {  
  10.         get   
  11.         {  
  12.             return carName;  
  13.         }  
  14.     }  
  15.     public BodyType CarBodyType  
  16.     {  
  17.         get;  
  18.         set;  
  19.     }  
  20.     public EngineType CarEngineType  
  21.     {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     public SeatType CarSeatType  
  26.     {  
  27.         get;  
  28.         set;  
  29.     }  
  30.   
  31.     public string GetDetails()   
  32.     {  
  33.         return string.Format("Name: {0}\nBodyType: {1}\nEngineType: {2}\nSeatType: {3}", CarName, CarBodyType, CarEngineType, CarSeatType);  
  34.     }  
  35. }  
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.
  1. interface ICarBuilder  
  2. {  
  3.     void BuildBody();  
  4.     void BuilEngine();  
  5.     void BuilSeats();  
  6.     Car MyCar  
  7.     {  
  8.         get;  
  9.     }  
  10. }  
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:
  1. class MarutiCarBuilder: ICarBuilder   
  2. {  
  3.     Car car = null;  
  4.     public MarutiCarBuilder()  
  5.     {  
  6.         car = new Car("Maruti");  
  7.     }  
  8.   
  9.     public void BuildBody()  
  10.     {  
  11.         car.CarBodyType = BodyType.HatchBack;  
  12.     }  
  13.   
  14.     public void BuildEngine()  
  15.     {  
  16.         car.CarEngineType = EngineType.Petrol;  
  17.     }  
  18.   
  19.     public void BuildSeats()  
  20.     {  
  21.         car.CarSeatType = SeatType.Regular;  
  22.     }  
  23.   
  24.     public CarMyCar   
  25.     {  
  26.         get  
  27.         {  
  28.             return car;  
  29.         }  
  30.     }  
  31. }  
  32.   
  33. class SkodaCarBuilder: ICarBuilder  
  34. {  
  35.     Car car = null;  
  36.     public SkodaCarBuilder()  
  37.     {  
  38.         car = new Car("Skoda");  
  39.     }  
  40.   
  41.     public void BuildBody()   
  42.     {  
  43.         car.CarBodyType = BodyType.Sedan;  
  44.     }  
  45.   
  46.     public void BuilEngine()   
  47.     {  
  48.         car.CarEngineType = EngineType.Diesal;  
  49.     }  
  50.   
  51.     public void BuilSeats()   
  52.     {  
  53.         car.CarSeatType = SeatType.SemiSleeper;  
  54.     }  
  55.   
  56.     public Car MyCar  
  57.     {  
  58.         get  
  59.         {  
  60.             return car;  
  61.         }  
  62.     }  
  63. }  
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.
  1. class CarManufacturer  
  2. {  
  3.     public void BuildCar(ICarBuildercarBuilder)   
  4.     {  
  5.         carBuilder.BuildBody();  
  6.         carBuilder.BuilEngine();  
  7.         carBuilder.BuilSeats();  
  8.     }  
  9. }  
We are done with the setup now. Let’s see how client uses this.
  1. static void Main()  
  2. {  
  3.         Console.Title = "Builder pattern demo";  
  4.         CarManufacturercarMfg = newCarManufacturer();  
  5.         ICarBuildercarBuilder = null;  
  6.   
  7.         ////Build Maruti Car  
  8.         carBuilder = newMarutiCarBuilder();  
  9.         carMfg.BuildCar(carBuilder);  
  10.         Console.WriteLine("Built Car Specs are:\n\n{0}", carBuilder.MyCar.GetDetails());  
  11.   
  12.         ////Build Skoda car  
  13.         carBuilder = newSkodaCarBuilder();  
  14.         carMfg.BuildCar(carBuilder);  
  15.         Console.WriteLine("Built Car Specs are:\n\n{0}", carBuilder.MyCar.GetDetails());  
  16. }  
Output

Output

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

Up Next
    Ebook Download
    View all
    Learn
    View all