Builder Design Pattern Real-time Example

Builder Design Pattern Real-time Example in C#

In this article, I am going to discuss the Builder Design Pattern Real-time Example in C#. Please read our previous article where we discussed the basics of Builder Design Pattern in C# with examples. Here, we will discuss the Beverage Example using the Builder Design Pattern.

Builder Design Pattern Real-time Example – Beverage

Let say, you want to prepare Coffee. In order to prepare coffee, you need to use components such as Water, Milk, Sugar, and Coffee Product. So, what you need to do is, you need to mix all these components and prepare coffee which is shown in the below image. So, here Coffee (Right-Hand Side) is the complex object and the small components (Milk, Water, Sugar, and Coffee product) are the simple objects. So, using these simple objects, you can prepare the complex object i.e. coffee.

Builder Design Pattern Real-time Example - Beverage

Suppose, you want to prepare Tea, then what you need to do is, you need to use Water, Milk, Sugar, and Tea Product. So, using these small objects (Water, Milk, Sugar, and Tea product) you can prepare the complex object i.e. Tea.

Builder Design Pattern Real-time Example in C#

As we already discussed the Builder Design Pattern is used to build a complex using a generic step by step process so that the same construction process can be used to build different representations of the same complex object.

If you observe in both the example, then you will see that the process of preparing Tea and Coffee is the same, only the content is different. So you can create one generic process something like below.

Real-time Example of Builder Design Pattern

Using the above generic process, now you can prepare Tea, Coffee, Horlicks, etc.

Implementation of Builder Design Pattern Real-time Example – Beverage

Let us implement the above real-time example using Builder Design Pattern in C# step by step.

Step1: Creating Product (Beverage)

Create a class file with the name Beverage.cs and then copy and paste the following code in it. This is going to be our product and in this class, we put all the attributes such as Water, Milk, Sugar, PowderQuantity, BeverageName which are common to prepare beverage (i.e. Coffee or Tea). We also create one method (i.e. ShowBeverage) which returns the beverage details.

namespace BuilderDesignPatternExample
{
    public class Beverage
    {
        public int Water { get; set; }
        public int Milk { get; set; }
        public int Sugar { get; set; }
        public int PowderQuantity { get; set; }
        public string BeverageName { get; set; }
        
        public string ShowBeverage()
        {
            return "Hot " + BeverageName + " [" + Water + " ml of water, " + Milk + "ml of milk, " + Sugar
                            + " gm of sugar, " + PowderQuantity + " gm of " + BeverageName + "]\n";
        }
    }
}
Step2: Creating Abstract Builder (BeverageBuilder)

Create a class file with the name BeverageBuilder.cs and then copy and paste the following in it. This BeverageBuilder class is going to be an abstract class and will act as a blueprint for any subclasses that want to create a Beverage. So, it will have different subclass implementations for different beverage types such as Coffee, Tea, Horlicks, etc. This class is having different abstract methods to set the Milk, Water, Sugar, PowderQuantity, and BeverageType.

namespace BuilderDesignPatternExample
{
    public abstract class BeverageBuilder
    {
        protected Beverage beverage;
        
        public void CreateBeverage()
        {
            beverage = new Beverage();
        }
        public Beverage GetBeverage()
        {
            return beverage;
        }
        
        public abstract void SetBeverageType();
        public abstract void SetWater();
        public abstract void SetMilk();
        public abstract void SetSugar();
        public abstract void SetPowderQuantity();
    }
}
Step3: Creating Concrete Builder (CoffeeBuilder and TeaBuilder)

In our example, we are going to create two types of beverages such as Tea and Coffee. So, we need to create two concrete builder classes by implementing the BeverageBuilder abstract class and providing implementation to all the abstract methods.

CoffeeBuilder.cs

Create a class file with the name CoffeeBuilder.cs and then copy and paste the following code in it. The following CoffeeBuilder class creates coffee by mixing the components such as water, milk, sugar, and coffee powder. This CoffeeBuilder class implements the BeverageBuilder abstract class which is the blueprint for creating the beverage objects.

using System;
namespace BuilderDesignPatternExample
{
    public class CoffeeBuilder : BeverageBuilder
    {
        public override void SetWater()
        {
            Console.WriteLine("Step 1 : Boiling water");
            GetBeverage().Water = 40;
        }

        public override void SetMilk()
        {
            Console.WriteLine("Step 2 : Adding milk");
            GetBeverage().Milk = 50;
        }

        public override void SetSugar()
        {
            Console.WriteLine("Step 3 : Adding Sugar");
            GetBeverage().Sugar = 10;
        }

        public override void SetPowderQuantity()
        {
            Console.WriteLine("Step 4 : Adding 15 Grams of coffee powder");
            GetBeverage().PowderQuantity = 15;
        }

        public override void SetBeverageType()
        {
            Console.WriteLine("Coffee");
            GetBeverage().BeverageName = "Coffee";
        }
    }
}

TeaBuilder.cs

Create a class file with the name TeaBuilder.cs and then copy and paste the following code in it. This class also implements the BeverageBuilder abstract class and provides implementation to all the abstract methods. The following TeaBuilder class is used to create Tea by mixing the components such as Water, Milk, Sugar, and Tea powder.

using System;
namespace BuilderDesignPatternExample
{
    public class TeaBuider : BeverageBuilder
    {
        public override void SetWater()
        {
            Console.WriteLine("Step 1 : Boiling water");
            GetBeverage().Water = 50;
        }

        public override void SetMilk()
        {
            Console.WriteLine("Step 2 : Adding milk");
            GetBeverage().Milk = 60;
        }

        public override void SetSugar()
        {
            Console.WriteLine("Step 3 : Adding Sugar");
            GetBeverage().Sugar = 15;
        }

        public override void SetPowderQuantity()
        {
            Console.WriteLine("Step 4 : Adding 20 Grams of coffee powder");
            GetBeverage().PowderQuantity = 20;
        }

        public override void SetBeverageType()
        {
            Console.WriteLine("Tea");
            GetBeverage().BeverageName = "Tea";
        }
    }
}
Step4: Creating the Director (BeverageDirector)

Once you created the concrete builder classes, then you need to need to create the director. The director is the component that will execute the required steps in a particular order to create a beverage.

So, create a class file with the name BeverageDirector.cs and then copy and paste the following code in it. This class is having one generic method which will take BeverageBuilder as an input parameter and then create and return a particular beverage object.

namespace BuilderDesignPatternExample
{
    public class BeverageDirector
    {
        public Beverage MakeBeverage(BeverageBuilder beverageBuilder)
        {
            beverageBuilder.CreateBeverage();

            beverageBuilder.SetBeverageType();
            beverageBuilder.SetWater();
            beverageBuilder.SetMilk();
            beverageBuilder.SetSugar();
            beverageBuilder.SetPowderQuantity();
            
            return beverageBuilder.GetBeverage();
        }
    }
}

Note: This MakeBeverage is so generic that it can create and return different types of Beverage objects.

Step5: Client Code.

Please modify the Main method as shown below. Here, first, we will create an instance of the BeverageDirector class and then create an instance of the TeaBuilder class. Once we have the BeverageDirector and TeaBuilder instance, then we call the MakeBeverage method on the BeverageDirector instance by passing the TeaBuilder instance as an argument which will create and return the tea. Again you need to follow the same process to make coffee.

using System;
namespace BuilderDesignPatternExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Beverage beverage;
            BeverageDirector beverageDirector = new BeverageDirector();
            
            TeaBuider tea = new TeaBuider();
            beverage = beverageDirector.MakeBeverage(tea);
            Console.WriteLine(beverage.ShowBeverage());

            CoffeeBuilder coffee = new CoffeeBuilder();
            beverage = beverageDirector.MakeBeverage(coffee);
            Console.WriteLine(beverage.ShowBeverage());

            Console.ReadKey();
        }
    }
}

Output:

Implementation of Builder Design Pattern Real-time Example - Beverage

In the next article, I am going to discuss the Fluent Interface Design Pattern in C# with some examples. Here, in this article, I try to explain one Real-time example of the Builder Design Pattern using C# as the programming language. I hope you understood the need and use of the Builder Design Pattern in real-time examples.

Leave a Reply

Your email address will not be published. Required fields are marked *