A day with .Net

My day to day experince in .net

New vending machine at office and Builder Design Pattern

Posted by vivekcek on June 28, 2014

Strong tea is my weakness, I tried all variations of tea’s described in the book ‘A Nice Cup of Tea’ by George Orwell.I recommend a strong tea over beer.

OK come to story, I was looking for a real world example for builder pattern.

So what is this Builder pattern according to GOF.

“Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

Ok so builder pattern deals with construction of objects, that means its a Creational pattern.

I went through some diagrams in internet and found this one useful.

build1

So what you understand from the above image, The definition of builder pattern says we have to reuse the mechanism to construct a complex object.

In the above image the complex object is product, and Director is responsible for creating this product.

So we have to design in such a way to reuse the Director to create different complex objects.

I was looking for a practical example to explain this to my readers. Then my friend Krishna and Mankunnu Subash came to my desk and said there is new vending machine installed at our office. Basically i don’t like the tea or coffee from such machine, because i am a fan of manually prepared tea. Anyway i go out with them.

Woww this is the machine.

build2

Krishna clicked the coffee switch, he got coffee,I think subash clicked the tea button.I selected Cappuccino, It was ok.

Then a bulb blown above my head, yes this machine can be used to explain builder pattern.

I would like to present this machine as a drink preparing machine, So the end product is a drink, which may be either tea or coffee.

build3

First of all our end product drink can be represented by the below class.

class Drink
{
    string _label;

    public Drink(string label)
    {
        this._label = label;
    }

    public override string ToString()
    {
        return this._label;
    }
}

Now i will define an interface.Which represent the process for building a drink.
A tea can be made by mixing hot water and tea powder, coffee can be made by mixing hot water and coffee powder.

interface IDrinkBuilder
{
    void AddWater();
    void AddPowder();
    Drink Drink { get; }
}

Now we will create a concrete tea builder as below.

class TeaBuilder : IDrinkBuilder
{

    Drink drink;

    public TeaBuilder()
    {
        drink = new Drink("Tea");
    }

    public void AddWater()
    {

    }

    public void AddPowder()
    {
        //Tea Powder
    }

    public Drink Drink
    {
        get { return drink; }
    }
}

Then a concrete coffee builder.

class CoffeeBuilder : IDrinkBuilder
{
    Drink drink;

    public CoffeeBuilder()
    {
        drink = new Drink("Coffee");
    }

    public void AddWater()
    {

    }

    public void AddPowder()
    {
        // Coffee powder
    }

    public Drink Drink
    {
        get { return drink; }
    }
}

Now we will design our Director, which can be reused to build a tea or coffee.

class DrinkMaker
{
    public void MakeDrink(IDrinkBuilder builder)
    {
        builder.AddWater();
        builder.AddPowder();
    }
}

You can use the above class in your main as below.

class Program
    {
        static void Main(string[] args)
        {
            DrinkMaker maker;
            IDrinkBuilder builder;

            Console.WriteLine("Enter 'T' for Tea and 'C' for coffee.");
            maker = new DrinkMaker();

            while (true)
            {
                string input = Console.ReadLine();

                if (input == "T")
                {
                    builder = new TeaBuilder();
                    maker.MakeDrink(builder);
                    Console.WriteLine(builder.Drink.ToString() + " is ready.");
                    Console.ReadLine();
                }
                else if (input == "C")
                {
                    builder = new CoffeeBuilder();
                    maker.MakeDrink(builder);
                    Console.WriteLine(builder.Drink.ToString() + " is ready.");
                    Console.ReadLine();
                }
                else
                {
                    Environment.Exit(0);
                }
            }

        }

    }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s