-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
122 lines (119 loc) · 2.48 KB
/
main2.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
// Product class
class Pizza {
public:
void setDough(const std::string& dough)
{
this->dough = dough;
}
void setSauce(const std::string& sauce)
{
this->sauce = sauce;
}
void setTopping(const std::string& topping)
{
this->topping = topping;
}
void displayPizza() const
{
std::cout << "Pizza with Dough: " << dough
<< ", Sauce: " << sauce
<< ", Topping: " << topping << std::endl;
}
private:
std::string dough;
std::string sauce;
std::string topping;
};
// Abstract builder class
class PizzaBuilder {
public:
virtual void buildDough() = 0;
virtual void buildSauce() = 0;
virtual void buildTopping() = 0;
virtual Pizza getPizza() const = 0;
};
// Concrete builder for a specific type of pizza
class HawaiianPizzaBuilder : public PizzaBuilder {
public:
void buildDough() override
{
pizza.setDough("Pan Dough");
}
void buildSauce() override
{
pizza.setSauce("Hawaiian Sauce");
}
void buildTopping() override
{
pizza.setTopping("Ham and Pineapple");
}
Pizza getPizza() const override { return pizza; }
private:
Pizza pizza;
};
// Concrete builder for another type of pizza
class SpicyPizzaBuilder : public PizzaBuilder {
public:
void buildDough() override
{
pizza.setDough("Thin Dough");
}
void buildSauce() override
{
pizza.setSauce("Spicy Tomato Sauce");
}
void buildTopping() override
{
pizza.setTopping("Pepperoni and Jalapenos");
}
Pizza getPizza() const override { return pizza; }
private:
Pizza pizza;
};
class ChodakPizzaBuilder : public PizzaBuilder {
public:
void buildDough() override
{
pizza.setDough("Fat Dough");
}
void buildSauce() override
{
pizza.setSauce("Mild Tomato Sauce");
}
void buildTopping() override
{
pizza.setTopping("Pepperoni");
}
Pizza getPizza() const override { return pizza; }
private:
Pizza pizza;
};
// Director class that orchestrates the construction
class Cook {
public:
void makePizza(PizzaBuilder& builder)
{
builder.buildDough();
builder.buildSauce();
builder.buildTopping();
}
};
int main()
{
Cook cook;
HawaiianPizzaBuilder hawaiianBuilder;
cook.makePizza(hawaiianBuilder);
Pizza hawaiianPizza = hawaiianBuilder.getPizza();
hawaiianPizza.displayPizza();
SpicyPizzaBuilder spicyBuilder;
cook.makePizza(spicyBuilder);
Pizza spicyPizza = spicyBuilder.getPizza();
spicyPizza.displayPizza();
ChodakPizzaBuilder chodakBuilder;
cook.makePizza(chodakBuilder);
Pizza chodakPizza = chodakBuilder.getPizza();
chodakPizza.displayPizza();
return 0;
}