Builder Design Pattern

What is builder Pattern?

It is a creational design pattern that creates an object in accordance with composites. A resultant object may contain one or more object(Say Sub-Object) and these Sub-Object may be created by other design patterns such as factory and Singleton etc. 
-*- Main idea is to keep construction of object and Implementation of object at separate location
-*- It is good to use builder patter to create an object if object contain some flat data such as HTML code, XML etc.
-*- Builder pattern is best pick where we are going to create a complex object and part of that complex object can be created independently and also hides the details from client how these are assembled.

In layman term this pattern is similar to block game which we used to play in childhood, where we create small-small blocks then join then one by one to create our structure/construction.

Formally there are 4 participant in Builder Pattern
1)Director : Creates objects using Builder Interface.
2)Builder : Used to create part of product, generally acts as an abstract.
3)Concrete Builder: Implements functionality of builder.
4)Product: Output product created by builder.

Example:


Typical example of Builder Pattern
                                             

Lets see how above mentioned participant are interrelated, if we take an example of pizza place. Where customer (Client) enters restaurant and gives it order to Attendant (Director) and pays its amount, now Attendant (Director) tells complete requirement to Cook (Builder), according to requirement cook select a oven/maker (Concrete Builder), oven/maker shall give pizza (Product), now Cook (Builder) serves pizza to Customer (Client)
Formally
1)Client create Directors object and configure it with desired Builder's object
2)Director also informs Builder when and what part of product to be build
3)Buider works on request taking help of concrete builder to add part to product
4)Client gets product from Builder on completion


Now let see how we can implement using c++




#include<iostream>
using namespace std;
//Product
class Pizza
{
        string m_baseType;
        string m_saucing;
        string m_toppings;
public:
        void Base(string base)
        {
                m_baseType=base;
        }
        void Sauce(string souce)
        {
                m_saucing=souce;
        }
        void Topping(string topping)
        {
                m_toppings=topping;
        }
        void serve()
        {
                cout<<"Here is Pizza with "<<m_baseType<<" Base, "<< m_saucing<<" Sauce and "<< m_toppings << " Toppings"<<endl;
        }
};
//Builder
class Cook
{
public:
        Cook(){}
        ~Cook(){}
protected:
        Pizza m_pizza;
public:
        virtual void makePizza(){}
        Pizza getPizza()
        {
                return m_pizza;
        }  
};
//Concrete Builder
class CookItalianPizza : public Cook
{
public:
        CookItalianPizza()
        {}
        void makePizza()
        {
                cout<<"Making Italian Pizza"<<endl;
                m_pizza.Base("Round_Shaped");
                m_pizza.Sauce("Red Chilly");
                m_pizza.Topping("Pepper and Capsicum");
        }
};
//Concrete Builder
class CookClassicPizza : public Cook
{
public:
        CookClassicPizza()
        {}
        void makePizza()
        {
                cout<<"Making Classic Pizza"<<endl;
                m_pizza.Base("Triangle_Shaped");
                m_pizza.Sauce("Tomato");
                m_pizza.Topping("Cheese, Pepper and Capsicum");
        }
};
//Director
class HotelManager
{
public:
        Cook* placeOrder(string cookPizzaType)
        {
                if(!cookPizzaType.compare("Italian"))
                {
                        return new CookItalianPizza();
                }
                if(!cookPizzaType.compare("Classic"))
                {
                        return new CookClassicPizza();
                }
        }
};
int main()
{
        //Client
        HotelManager* Manager=new HotelManager();
        Cook* cookPizzType=Manager->placeOrder("Italian");
        cookPizzType->makePizza();
        Pizza pizza=cookPizzType->getPizza();
        pizza.serve();

        cookPizzType=Manager->placeOrder("Classic");
        cookPizzType->makePizza();
        Pizza classicPizz=cookPizzType->getPizza();
        classicPizz.serve();
        return 0;
}                                  



Output

Making Italian Pizza
Here is Pizza with Round_Shaped Base, Red Chilly Sauce and Pepper and Capsicum Toppings
Making Classic Pizza
Here is Pizza with Triangle_Shaped Base, Tomato Sauce and Cheese, Pepper and Capsicum Toppings


PREV::Abstract Factory with Singleton

Your Comments /Suggestions and Questions are always welcome,  shall clarify with best of knowledge. So feel free to put Questions.

No comments:

Post a Comment