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.

Working with Abstract Factory Pattern

Hello friends.

Today I am here to go though another variant of Factory design pattern called Abstract Factory. 

In case, you have not had a look at my previous article about Factory pattern, I would suggest you to look for them in ARCHIVES section.

Before talking about its implementation let’s try to understand the purpose of the abstract factory pattern

Abstract factory pattern is next level to Factory Method pattern where we define related factories or families of classes.

To understand this properly, let’s take the same example we took when going through Simple Factory pattern.

In Factory Method pattern, we managed all the car brands with one interface. Now let’s assume consumer or client of the car wants next level of categorization by picking any car brand. Then how will you manage?

Here comes the Abstract Factory. It creates family of classes or related factories to address the need.

Enough theory. Now let’s talk about implementation.

How to use Abstract Factory Pattern?

Let’s try to understand using simple example.

Suppose client wants to know the car details of different kinds of brands. Car detail here is about knowing economy and premium models for any brand. For that we need to define the abstract product interface as in the following.

  • interface IEconomy  
  • {  
  •     string ShowEconomy();  
  • }  
  • interface IPremium  
  • {  
  •     string ShowPremium();  
  • }  

Now since we need to show the car details by brand, we need to create concrete product classes using the interface defined above.

  • class Alto: IEconomy  
  • {  
  •     public string ShowEconomy()  
  •     {  
  •         return “Alto”;  
  •     }  
  • }  
  • class SCross: IPremium  
  • {  
  •     public string ShowPremium()  
  •     {  
  •         return “S-Cross”;  
  •     }  
  • }  
  • class Eon: IEconomy  
  • {  
  •     public string ShowEconomy()  
  •     {  
  •         return “Eon”;  
  •     }  
  • }  
  • class Elantra: IPremium  
  • {  
  •     public string ShowPremium()  
  •     {  
  •         return “Elantra”;  
  •     }  
  • }  

Now we need to define an abstract factory interface, which can accommodate the abstract product interfaces created to define car details as in the following:

  • interface ICarFactory  
  • {  
  •    IEconomy GetEconomy();  
  •    IPremium GetPremium();  
  • }  

Now the time has come to create the concrete family of classes to support different brands, of course by implementing the ICarFactory.

  • class MarutiFactory: ICarFactory  
  • {  
  •     public IEconomy GetEconomy()  
  •     {  
  •         return new Alto();  
  •     }  
  •     public IPremium GetPremium()  
  •     {  
  •         return new SCross();  
  •     }  
  • }  
  • class HyundaiFactory: ICarFactory  
  • {  
  •     public IEconomy GetEconomy()  
  •     {  
  •         return new Eon();  
  •     }  
  •     public IPremium GetPremium()  
  •     {  
  •         return new Elantra();  
  •     }  
  • }  

And now we have reached a point where we can use the setup created so far. Let’s do it as in the following:

  • Console.Title = “Abstract Factory pattern demo”;  
  • ICarFactory carFactory = null;
  • IEconomy economy = null;
  • IPremium premium = null;
  • //Maruti  
  • carFactory = new MarutiFactory();
  • economy = carFactory.GetEconomy();
  • premium = carFactory.GetPremium();
  • Console.WriteLine(“Normal car is: ” + economy.ShowEconomy() + “\nPremium Car is: ” + premium.ShowPremium()); 

Output:

run
  • //Hyundai  
  • carFactory = new HyundaiFactory();
  • economy = carFactory.GetEconomy();
  • premium = carFactory.GetPremium();
  • Console.WriteLine(“Normal car is: ” + economy.ShowEconomy() + “\nPremium Car is: ” + premium.ShowPremium()); 

Output:

Output

 And here goes the class diagram. 

As you can see in the above code that client is getting the required car factory object by just creating the instance of respective car factory brand. And then it gets the required interface object by calling it’s respective car factory method in order to further call the product interface methods i.e. ShowEconomy and ShowPremium. 

Hope you have liked the article. Look forward for your comments/suggestions. 

Working with Factory Method Pattern

Hello friends,

Today we will go through another creational design pattern called Factory Method.

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

Factory method pattern is similar to simple Factory pattern with a few difference. Unlike Simple factory, here client doesn’t call the factory class instead it calls the factory interface. Factory interface further have concrete implementations where object creation is performed.

Now let’s talk about implementation.

How to use Factory Method Pattern

Let’s try to understand using simple example.

Assume the client wants to know the on-road price of various brands of cars and have an interface as in the following,

  • interface ICar
  • {
  •    string GetOnRoadPrice(string model);
  • }

And here goes the concrete implementation of the ICar.

  • class Maruti : ICar
  • {
  •     public string GetOnRoadPrice(string model)
  •     {
  •         //Logic to get OnRoadPrice based on model
  •         if (model == “Alto 800 VXI”)
  •         {
  •             return “3.4 Lakhs INR”;
  •         }
  •         else
  •         {
  •             return “Information not available!”;
  •         }
  •     }
  • }
  • class Hyundai : ICar
  • {
  •     public string GetOnRoadPrice(string model)
  •     {
  •         //Logic to get OnRoadPrice based on model
  •         if (model == “Grand i10 Magna 1.2 BSV”)
  •         {
  •             return “5.4 Lakhs INR”;
  •         }
  •         else
  •         {
  •             return “Information not available!”;
  •         }
  •     }
  • }

We need to create a Factory Interface now which will sit between client and business layers and it will provide the required factory object to the client.

  • interface ICarFactory
  • {
  •    ICar GetCar();
  • }

And here goes the concrete implementation of the ICarFactory.

  • class MarutiFactory : ICarFactory
  • {
  •     public ICar GetCar()
  •     {
  •         return new Maruti();
  •     }
  • }
  • class HyundaiFactory : ICarFactory
  • {
  •     public ICar GetCar()
  •     {
  •         return new Hyundai();
  •     }
  • }

Now let’s see, how client can use the setup we have created so far.

  • //Client side
  • Console.Title = “Factory method pattern demo”;
  • ICarFactory carFactory = null;
  • ICar car = null;
  • string model = null;
  • //Maruti
  • carFactory = new MarutiFactory();
  • car = carFactory.GetCar();
  • model = “Alto 800 VXI”;
  • Console.WriteLine(“On-road price for {0} car is: {1} “, model, car.GetOnRoadPrice(model));

Output

run

  • //Hyundai
  • carFactory = new HyundaiFactory();
  • car = carFactory.GetCar();
  • model = “Grand i10 Magna 1.2 BSV”;
  • Console.WriteLine(“On-road price for {0} car is: {1} “, model, car.GetOnRoadPrice(model));

Output

Output

And here goes the class diagram.

class diagram

As you can see in the above code that,

  • Client is getting the required factory object by just creating instance of respective car factory brand.
  • And then it gets the required car object by calling GetCar method of retrieved factory object.
  • Client gets the On-road price of the car by calling GetOnRoadPrice method and passing model name.

So just to summarize, using Factory Method pattern, we achieved the following,

  • Loose coupling between client and business layers.
  • Client is only aware about Factory Interface (ICarFactory) and not their concrete implementations.
  • Abstracted concreate classes (Maruti, Hyundai) from client.

Hope you have liked the article. Looking forward for your comments/suggestions.