Builder Design Pattern Real-Time Example in C#

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 the Builder Design Pattern in C# with simple Examples. Here, we will discuss the Beverage Example using the Builder Design Pattern using C# Language.

Builder Design Pattern Real-Time Example in C# – Beverage

Let’s say, you want to prepare Coffee. In order to prepare coffee, you need to use components such as Water, Milk, Sugar, and Coffee Products. 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 a complex object i.e. coffee.

Builder Design Pattern Real-time Example in C#

Suppose, you want to prepare Tea, then what you need to do is, you need to use Water, Milk, Sugar, and Tea Products. So, using these small objects (Water, Milk, Sugar, and Tea Products) you can prepare a complex object i.e. Tea which is shown in the below image.

Builder Pattern Real-time Example C#

As we already discussed the Builder Design Pattern in C# is used to build a complex object using a generic step-by-step process so that the same construction process i.e. the same steps can be used to build different representations of the same complex object. If you observe both examples, 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 the one below.

Example of Builder Design Pattern in C#

Using the above generic process, now you can prepare Tea, Coffee, Horlicks, etc. You can implement the above in any programming language very easily by using the Builder Design Pattern. Let us proceed and see how we can implement the above Real-Time Application using Builder Design Pattern in C#.

Implementation of Builder Design Pattern Real-time Example in C# – Beverage

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

Step 1: Creating Product (Beverage)

It makes sense to use the Builder Design Pattern only when your products are quite complex and require extensive configuration. Create a class file with the name Beverage.cs and then copy and paste the following code into it. This is going to be our product and in this class, we put all the attributes such as Water, Milk, Sugar, PowderQuantity, and BeverageName which are common to prepare beverages (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";
        }
    }
}
Step 2: Creating Abstract Builder (BeverageBuilder)

The Builder Abstract Class specifies methods for creating the different parts of the Product objects. Create a class file with the name BeverageBuilder.cs and then copy and paste the following into 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();
    }
}
Step 3: Creating Concrete Builder (CoffeeBuilder and TeaBuilder)

In our example, we are going to create two types of beverages i.e. 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 into 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 into 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";
        }
    }
}
Step 4: Creating the Director (BeverageDirector)

Once you created the concrete builder classes, then you need to create the director. The Director is only responsible for executing the building steps in a particular order. It is helpful when producing products according to a specific order or configuration.

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 into 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 Method is so generic that it can create and return different types of Beverage objects.

Step 5: 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 instances, then we call the MakeBeverage method on the BeverageDirector instance bypassing the TeaBuilder instance as an argument that will create and return the tea. Again we 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 in C#.

Leave a Reply

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