I am here to continue the discussion around Design Patterns. Today we will discuss another creational design pattern called Builder.
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.
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;
-
-
- carBuilder = newMarutiCarBuilder();
- carMfg.BuildCar(carBuilder);
- Console.WriteLine("Built Car Specs are:\n\n{0}", carBuilder.MyCar.GetDetails());
-
-
- carBuilder = newSkodaCarBuilder();
- carMfg.BuildCar(carBuilder);
- Console.WriteLine("Built Car Specs are:\n\n{0}", carBuilder.MyCar.GetDetails());
- }
Output
I hope you have liked the article. I look forward to your comments/suggestions.