-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.cpp
More file actions
69 lines (61 loc) · 1.68 KB
/
Builder.cpp
File metadata and controls
69 lines (61 loc) · 1.68 KB
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
//
// Created by osher on 14/02/2022.
//
#include <iostream>
using namespace std;
class Pizza {
friend class PizzaBuilder;
const bool _extra_cheese;
const bool _tomatoes;
const bool _olives;
const bool _pineapple;
Pizza(bool extra_cheese, bool tomatoes, bool olives, bool pineapple):_extra_cheese(extra_cheese), _olives(olives), _pineapple(pineapple), _tomatoes(tomatoes) {}
public:
void print() const {
cout << "Pizza states:" << endl;
cout << "\tExtra Cheese: " << (_extra_cheese ? "+" : "-") << endl;
cout << "\tTomatoes: " << (_tomatoes ? "+" : "-") << endl;
cout << "\tOlives: " << (_olives ? "+" : "-") << endl;
cout << "\tPineapple: " << (_pineapple ? "+" : "-") << endl;
}
};
class PizzaBuilder {
bool _extra_cheese;
bool _tomatoes;
bool _olives;
bool _pineapple;
public:
PizzaBuilder(){
_extra_cheese = false;
_tomatoes = false;
_olives = false;
_pineapple = false;
}
PizzaBuilder AddCheese() {
_extra_cheese = true;
return *this;
}
PizzaBuilder AddTomatoes() {
_tomatoes = true;
return *this;
}
PizzaBuilder AddOlives() {
_olives = true;
return *this;
}
PizzaBuilder AddPineapple() {
_pineapple = true;
return *this;
}
Pizza* build() const {
return new Pizza(_extra_cheese, _tomatoes, _olives, _pineapple);
}
};
int main() {
PizzaBuilder pizzaBuilder;
Pizza p = *(pizzaBuilder.AddCheese().AddOlives().AddPineapple().build());
p.print();
Pizza* p2 = pizzaBuilder.AddCheese().AddOlives().AddPineapple().build();
p2->print();
return 0;
}