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:
- Design Patterns Simplified - Part 1
- Design Patterns Simplified - Part 2 (Singleton)
- Design Patterns Simplified - Part 3 (Simple Factory)
- Design Patterns Simplified - Part 4 (Abstract Factory)
- Design Patterns Simplified - Part 5 (Factory Method)
- Design Patterns Simplified - Part 6 (Prototype)
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.
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.
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.
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:
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.
We are done with the setup now. Let’s see how client uses this.
Output

I hope you have liked the article. I look forward to your comments/suggestions.
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
- }
- 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);
- }
- }
- interface ICarBuilder
- {
- void BuildBody();
- void BuilEngine();
- void BuilSeats();
- Car MyCar
- {
- get;
- }
- }
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;
- }
- }
- }
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();
- }
- }
- 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());
- }

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

Rajan MishraPosted Oct 10, 2018, 1:37 AM
Hey prakash it is the most simple and complete representation of Builder Pattern. I have selected it for my personal notes.
Prakash TripathiPosted Apr 11, 2016, 3:42 AM
Thnx Humayun.
Humayun Kabir MamunPosted Apr 11, 2016, 3:40 AM
Nice...
Prakash TripathiPosted Apr 9, 2016, 10:41 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:36 AM
Nice article..
Prakash TripathiPosted Mar 6, 2016, 11:09 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 11:09 PM
Thnx Sr Karthiga.
Kumaresh RajalingamPosted Mar 6, 2016, 8:30 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:30 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:19 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:19 PM
Nice explanation
Prakash TripathiPosted Jan 18, 2016, 9:46 AM
Thnx Ibrahim.
Mohammed IbrahimPosted Jan 18, 2016, 9:26 AM
nice
Prakash TripathiPosted Jan 18, 2016, 1:39 AM
Thnx Ankit.
Ankit BansalPosted Jan 18, 2016, 12:35 AM
Really helpful..
Prakash TripathiPosted Jan 17, 2016, 2:17 PM
Thnx Santhakumar.
Santhakumar MunuswamyPosted Jan 17, 2016, 2:01 PM
Thanks for nice share
Prakash TripathiPosted Jan 17, 2016, 1:47 PM
Thnx Ankur.
Ankur MistryPosted Jan 17, 2016, 10:23 AM
Nice share