Builder Design Pattern

Home / Builder Design Pattern

Builder Design Pattern

December 11, 2015 | Article | No Comments

Builder is a creational design pattern, used to separate the construction of a complex object from its representation so that the same construction process can create different objects representations.

Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern.

Problem

We want to construct a complex object, however we do not want to have a complex constructor member or one that would need many arguments.

Solution

Define an intermediate object whose member functions define the desired object part by part before the object is available to the client. Builder pattern lets us defer the construction of the object until all the options for creation have been specified.

Example

#include <string>
#include <iostream>
using namespace std;

// "Product"
class Pizza {
	string m_dough;
	string m_sauce;
	string m_topping;
public:
	void setDough(const string& dough)
	{
		m_dough = dough;
	}
	void setSauce(const string& sauce)
	{
		m_sauce = sauce;
	}
	void setTopping(const string& topping)
	{
		m_topping = topping;
	}
	void open() const
	{
		cout <<  "Pizza with " << m_dough << " dough, " << m_sauce << 
			" sauce and " << m_topping << " topping. Mmm." << endl;
	}
};

// "Abstract Builder"
class PizzaBuilder {
public:
	Pizza* getPizza()
	{
		return m_pizza;
	}
		void createNewPizzaProduct()
	{
		m_pizza = new Pizza;
	}
	virtual void buildDough() = 0;
	virtual void buildSauce() = 0;
	virtual void buildTopping() = 0;
protected:
	Pizza* m_pizza;
};
//----------------------------------------------------------------
class HawaiianPizzaBuilder : public PizzaBuilder {
public:
	virtual void buildDough()
	{
		m_pizza->setDough("cross");
	}
	virtual void buildSauce()
	{
		m_pizza->setSauce("mild");
	}
	virtual void buildTopping()
	{
		m_pizza->setTopping("ham+pineapple");
	}
};
class SpicyPizzaBuilder : public PizzaBuilder {
public:
	virtual void buildDough()
	{
		m_pizza->setDough("pan baked");
	}
	virtual void buildSauce()
	{
		m_pizza->setSauce("hot");
	}
	virtual void buildTopping()
	{
		m_pizza->setTopping("pepperoni+salami");
	}
};
//----------------------------------------------------------------
class Cook {
	PizzaBuilder* m_pizzaBuilder;
public:
	void setPizzaBuilder(PizzaBuilder* pb)
	{
		m_pizzaBuilder = pb;
	}
	Pizza* getPizza()
	{
		return m_pizzaBuilder->getPizza();
	}
	void constructPizza()
	{
		m_pizzaBuilder->createNewPizzaProduct();
		m_pizzaBuilder->buildDough();
		m_pizzaBuilder->buildSauce();
		m_pizzaBuilder->buildTopping();
	}
};
int main() {
	Cook cook;
	PizzaBuilder* hawaiianPizzaBuilder = new HawaiianPizzaBuilder;
	PizzaBuilder* spicyPizzaBuilder = new SpicyPizzaBuilder;
	cook.setPizzaBuilder(hawaiianPizzaBuilder);
	cook.constructPizza();
	Pizza* hawaiian = cook.getPizza();
	hawaiian->open();
	cook.setPizzaBuilder(spicyPizzaBuilder);
	cook.constructPizza();
	Pizza* spicy = cook.getPizza();
	spicy->open();

	delete hawaiianPizzaBuilder;
	delete spicyPizzaBuilder;
	delete hawaiian;
	delete spicy;
}

Notes

Pattern in Action

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

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

Social media & sharing icons powered by UltimatelySocial