-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.cpp
126 lines (92 loc) · 2.79 KB
/
Builder.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
123
124
125
126
//============================================================================
// Name : Builder.cpp
// Created on : 19.04.2020
// Author : Tokmakov Andrey
// Version : 1.0
// Copyright : Your copyright notice
// Description : Builder design pattern tests
//============================================================================
#include "Builder.h"
namespace Builder_Pattern_Tests {
Pizza::Pizza(){
}
Pizza::~Pizza()
{
}
void Pizza::setName(CString name) noexcept {
this->name = name;
}
void Pizza::setDough(CString dough) noexcept {
this->dough = dough;
}
void Pizza::setSauce(CString sauce) noexcept {
this->sauce = sauce;
}
void Pizza::setTopping(CString topping) noexcept {
this->topping = topping;
}
String Pizza::getName() const noexcept {
return this->name;
}
std::shared_ptr<Pizza> PizzaBuilder::getPizza() noexcept {
return this->pizza;
}
void PizzaBuilder::createNewPizzaProduct() {
this->pizza = std::shared_ptr<Pizza>(new Pizza);
}
void HawaiianPizzaBuilder::setName() {
this->pizza->setName("HawaiianPizzaBuilder");
}
void HawaiianPizzaBuilder::buildDough() {
this->pizza->setDough("cross");
}
void HawaiianPizzaBuilder::buildSauce() {
this->pizza->setSauce("mild");
}
void HawaiianPizzaBuilder::buildTopping() {
this->pizza->setTopping("ham + pineapple");
}
void SpicyPizzaBuilder::setName() {
this->pizza->setName("SpicyPizzaBuilder");
}
void SpicyPizzaBuilder::buildDough() {
this->pizza->setDough("pan baked");
}
void SpicyPizzaBuilder::buildSauce() {
this->pizza->setSauce("hot");
}
void SpicyPizzaBuilder::buildTopping() {
this->pizza->setTopping("pepperoni + salami");
}
void Waiter::setPizzaBuilder(std::shared_ptr<PizzaBuilder> pizzaBuilder) noexcept {
this->pizzaBuilder = pizzaBuilder;
}
std::shared_ptr<Pizza> Waiter::getPizza() noexcept {
return this->pizzaBuilder->getPizza();
}
void Waiter::constructPizza() noexcept {
pizzaBuilder->createNewPizzaProduct();
pizzaBuilder->setName();
pizzaBuilder->buildDough();
pizzaBuilder->buildSauce();
pizzaBuilder->buildTopping();
}
void Test()
{
Waiter* waiter = new Waiter();
{
std::shared_ptr<PizzaBuilder> builder = std::make_shared<HawaiianPizzaBuilder>();
waiter->setPizzaBuilder(builder);
waiter->constructPizza();
std::shared_ptr<Pizza> pizza = waiter->getPizza();
std::cout << "Pizza: " << pizza->getName() << std::endl;
}
{
std::shared_ptr<PizzaBuilder> builder = std::make_shared<SpicyPizzaBuilder>();
waiter->setPizzaBuilder(builder);
waiter->constructPizza();
std::shared_ptr<Pizza> pizza = waiter->getPizza();
std::cout << "Pizza: " << pizza->getName() << std::endl;
}
}
};