Learn Design Pattern - Builder Pattern

In this article we will talk about the Builder Pattern.

In this article we will talk about the Builder Pattern.

Agenda

  • What is Builder Pattern?
  • Create McDonald Happy Meal Program.
  • Understand the problems with traditional programming style.
  • Try some various solutions and there cons.
  • See How Builder Pattern solves all problems.
  • What are the components involved in the Builder Pattern?
  • Create the Code Using the Builder Pattern.

Previous Articles

  1. Design Patterns: Introduction
  2. Learn Design Pattern - Singleton Pattern
  3. Learn Design Pattern - Factory Method Pattern
  4. Learn Design Pattern - Abstract Factory Pattern

What is Builder Pattern?

  • It falls under the creational pattern.
  • This pattern is handy when construction of an object is complex (Creating object includes, creating instance + assigning values to some members) and you want to separate the construction logic from the representation so that it can be used with various representations.
  • The Factory pattern is more of an answer to: "What is being built?", whereas this pattern is more of an answer to: "How to built it?".

Problem Statement

You all may have heard of McDonald's Happy Meals. Let's create a code for creating a ChickenMcGrill HappyMeal.

A Happy Meal normally consists of 3 things; a burger, a cold drink and a toy.

Step 1

1. Create ChickenMcGrill Class - Burger

  1. public class ChickenMcGrill : IBurger  
  2. {
  3. }

2. Create Coke Class - Cold drink

  1. public class Coke : IColdDrink  
  2. {  
  3. }  

 

3. Create IceAgeToy Class - Toy

  1. public class IceAgeToy : IToy  
  2. {  
  3. }  

 

Step 2
 
Create a HappyMeal as
  1. public class HappyMeal  
  2. {  
  3.     public IBurger Burger  
  4.     { getset; }  
  5.     public IColdDrink ColdDrink  
  6.     { getset; }  
  7.     public IToy Toy  
  8.     { getset; }  
  9. }  
Step 3

Create client code as

  1. HappyMeal objPriMeal = new HappyMeal();  
  2. objPriMeal.Burger = new ChickenMcGrill();  
  3. objPriMeal.ColdDrink = new Coke();  
  4. objPriMeal.Toy = new IceAgeToy();  
  5. Console.WriteLine(objPriMeal);  

 

Problems and various solutions

Problem 1

  • The End client has to be aware of the steps for building the final product.

  • A new Happy meal, say for a McVeggie, results in a substantial amount of cluttered code
    (because now ChickenMcGrill will be replaced with McVeggie and Toy with HarryPotterToy).

Solution 1

Change the constructor logic of the Happy Meal as:

  1. public HappyMeal(int type)  
  2. {  
  3.     switch (type)  
  4.     {  
  5.         case 1:  
  6.             this.Burger = new ChickenMcGrill();  
  7.             this.ColdDrink = new Coke();  
  8.             this.Toy = new IceAgeToy();  
  9.             break;  
  10.         case 2:  
  11.             this.Burger = new McVeggie();  
  12.             this.ColdDrink = new Coke();  
  13.             this.Toy = new HarrPotterToy();  
  14.             break;  
  15.     }  
  16. }  

 

//ClientCode

  1. HappyMeal objPriMeal = new HappyMeal(1);  
  2. Console.WriteLine(objPriMeal);  
  3. HappyMeal objPriMeal = new HappyMeal(2);  
  4. Console.WriteLine(objPriMeal);  
Problem 2


Every new product results in a new case in the constructor.

Solution 2

Factory Method Pattern: Let the Factory create the final Happy Meal Object depending on the scenario.

Problem 3

We can see for every happy meal, the construction process is the same except some attributes are affected and so those representation steps need to be separated from the construction logic.

Solution 3 Builder Pattern

The Builder pattern separates out the representation and creation from each other with the help of Builders and Directors.

Components involved in the Builder pattern are

  • AbstractBuilder - Contains the steps required for creating final concrete object.

  • Builder - Constructs the Individual part of the Concrete Product implementing AbstractBuilder.

  • Director - Construct the complete Concrete Product using Builder.

  • Product - A Complex Object which is required to create.

Code Walkthrough

Step 1

Create the AbstractBuilder with all the necessary steps:

  1. public abstract class AbstractHappyMeal  
  2. {  
  3.     internal HappyMeal happyMeal;  
  4.     public void CreateHappyMeal()  
  5.     {  
  6.         happyMeal = new HappyMeal();  
  7.     }  
  8.     public HappyMeal GetHappyMeal()  
  9.     {  
  10.         return happyMeal;  
  11.     }  
  12.     public abstract void AddBurger();  
  13.     public abstract void AddColdDrink();  
  14.     public abstract void AddToy();  
  15. }  

 

Step 2

Create a Builder for ChickenMcGrill and McVegie:

  1. public class ChickenMcGrillHappyMeal : AbstractHappyMeal  
  2. {  
  3.     public override void AddBurger()  
  4.     {  
  5.         happyMeal.Burger = new ChickenMcGrill();  
  6.     }  
  7.     public override void AddColdDrink()  
  8.     {  
  9.         happyMeal.ColdDrink = new Coke();  
  10.     }  
  11.     public override void AddToy()  
  12.     {  
  13.         happyMeal.Toy = new IceAgeToy();  
  14.     }  
  15. }  
  16. public class McVeggieHappyMeal : AbstractHappyMeal  
  17. {  
  18.     public override void AddBurger()  
  19.     {  
  20.         happyMeal.Burger = new McVeggie();  
  21.     }  
  22.     public override void AddColdDrink()  
  23.     {  
  24.         happyMeal.ColdDrink = new Coke();  
  25.     }  
  26.     public override void AddToy()  
  27.     {  
  28.         happyMeal.Toy = new IceAgeToy();  
  29.     }  
  30. }  
Step 3

Create Director

  1. public class Director  
  2. {  
  3.     public HappyMeal GetHappyMeal(AbstractHappyMeal HappyMealBuilder)  
  4.     {  
  5.         HappyMealBuilder.CreateHappyMeal();  
  6.         HappyMealBuilder.AddBurger();  
  7.         HappyMealBuilder.AddColdDrink();  
  8.         HappyMealBuilder.AddToy();  
  9.         return HappyMealBuilder.GetHappyMeal();  
  10.     }  
  11. }  
Step 4

No more constructor logic or factory method is required.

Client Code

  1. AbstractHappyMeal objMealBuilder = new McAlooTikkiHappyMeal();  
  2. Director objDirector = new Director();  
  3. HappyMeal objMeal = objDirector.GetHappyMeal(objMealBuilder);  
  4.   
  5. objMealBuilder = new McVeggieHappyMeal();  
  6. objDirector = new Director();  
  7. objMeal = objDirector.GetHappyMeal(objMealBuilder);  
  8.   
  9. objMealBuilder = new ChickenMcGrillHappyMeal();  
  10. objDirector = new Director();  
  11. objMeal = objDirector.GetHappyMeal(objMealBuilder);  

 

Output of the preceding Code


builder-pattern.jpg

Class Diagram

Builder Pattern Class Diagram.jpg


Note:
Patterns are made for solving the business problems. If any pattern does not solve the problem then it would be better you revamp it as per your requirements.

Even in some scenario, 2 or more patterns are used together.

Please download both of the attached source codes for a complete demonstration of the pattern.
The second sample is all about implementing the builder pattern in ASP.Net and you will also find how two patterns (Factory and builder) work together.

Hope you enjoyed reading this article. Keep programming and supply some good comments.

Stay tuned for the next pattern.