-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.hpp
87 lines (77 loc) · 2.07 KB
/
builder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef BUILDER_HPP
#define BUILDER_HPP
#include <iostream>
#include <memory>
#include <string>
// Product
class Pizza
{
private:
std::string dough = "classic";
std::string sauce = "classic";
std::string topping = "classic";
public:
Pizza() { }
~Pizza() { }
void SetDough(const std::string& d) { dough = d; }
void SetSauce(const std::string& s) { sauce = s; }
void SetTopping(const std::string& t) { topping = t; }
void ShowPizza()
{
std::cout << "Yummy!!!" << std::endl
<< "Pizza with Dough as "<< dough
<< ", Sauce as " << sauce
<< " and Topping as "<< topping
<< " !"<< std::endl;
}
};
// Abstract Builder
class PizzaBuilder
{
protected:
std::shared_ptr<Pizza> pizza;
PizzaBuilder() {}
public:
std::shared_ptr<Pizza> GetPizza() { return pizza; }
void createNewPizzaProduct() { pizza.reset (new Pizza); }
virtual void buildDough() {}
virtual void buildSauce() {}
virtual void buildTopping() {}
};
// ConcreteBuilder
class HawaiianPizzaBuilder : public PizzaBuilder
{
public:
HawaiianPizzaBuilder() : PizzaBuilder() {}
~HawaiianPizzaBuilder(){}
void buildSauce() { pizza->SetSauce("mild");}
void buildTopping() { pizza->SetTopping( "ham and pineapple");}
};
// ConcreteBuilder
class SpicyPizzaBuilder : public PizzaBuilder
{
public:
SpicyPizzaBuilder() : PizzaBuilder() {}
~SpicyPizzaBuilder() {}
void buildDough() { pizza->SetDough( "pan baked");}
void buildSauce() { pizza->SetSauce( "hot");}
void buildTopping() { pizza->SetTopping( "pepperoni and salami"); }
};
// Director
class Waiter
{
private:
PizzaBuilder* pizzaBuilder; public:
Waiter() : pizzaBuilder(NULL) {}
~Waiter() { }
void SetPizzaBuilder(PizzaBuilder* b) { pizzaBuilder = b; }
std::shared_ptr<Pizza> GetPizza() { return pizzaBuilder->GetPizza(); }
void ConstructPizza()
{
pizzaBuilder->createNewPizzaProduct();
pizzaBuilder->buildDough();
pizzaBuilder->buildSauce();
pizzaBuilder->buildTopping();
}
};
#endif // BUILDER_HPP