-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreational-builder-1.php
229 lines (196 loc) · 5 KB
/
creational-builder-1.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* IMPORTANT NOTE: This example just for explain the pattern, but in real work you may need to do some action
** for example in house abstract or via union products or to use variable without setters and getters...
** and make more than one build, but this example just
* to simulate build Builder Pattern with fully Idea...
*
* This example simulate two products
*/
/**
* This is Builder interface
* 2nees.com
*/
interface HouseBuilderInterface
{
public function createWall(): void;
public function createRoof(): void;
public function createDoor(): void;
public function createWindow(): void;
public function createGarage(): void;
public function createSwimmingPool(): void;
public function getResult();
}
/**
* Simple Abstract Class to share common data between Products
* Note: its just for example, so that no setter or getter...etc
* 2nees.com
*/
abstract class HouseAbstracts {
public $wall;
public $roof;
public $door;
public $window;
public $garage;
public $swimmingPool;
public abstract function buildHome() : void;
}
/**
* This is Product, not require to follow the builder interface in all result
* 2nees.com
*/
class House extends HouseAbstracts {
public function buildHome(): void
{
echo "Build {$this->wall} walls" . PHP_EOL;
echo "Build roof" . PHP_EOL;
echo "Build {$this->door} doors" . PHP_EOL;
echo "Build {$this->window} window" . PHP_EOL;
echo "Building House Completed! Thank You!" . PHP_EOL;
}
}
/**
* This is Product, not require to follow the builder interface in all result
* 2nees.com
*/
class Castle extends HouseAbstracts {
public function buildHome(): void
{
echo "Build {$this->wall} walls" . PHP_EOL;
echo "Build roof" . PHP_EOL;
echo "Build {$this->door} doors" . PHP_EOL;
echo "Build {$this->window} window" . PHP_EOL;
echo "Build Garage" . PHP_EOL;
echo "Build Swimming Pool" . PHP_EOL;
echo "Building Castle Completed! Thank You!" . PHP_EOL;
}
}
/**
* This is Concrete Builder
* 2nees.com
*/
class HouseBuilder implements HouseBuilderInterface {
private House $house;
public function __construct()
{
$this->reset();
}
public function reset()
{
$this->house = new House();
}
public function createWall(): void
{
$this->house->wall = 4;
}
public function createRoof(): void
{
$this->house->roof = "Done!";
}
public function createDoor(): void
{
$this->house->door = 1;
}
public function createWindow(): void
{
$this->house->window = 2;
}
public function createGarage(): void
{
$this->house->garage = false;
}
public function createSwimmingPool(): void
{
$this->house->swimmingPool = false;
}
public function getResult(): House
{
$result = $this->house;
$this->reset();
return $result;
}
}
/**
* This is Concrete Builder
* 2nees.com
*/
class CastleBuilder implements HouseBuilderInterface {
private Castle $castle;
public function __construct()
{
$this->reset();
}
public function reset()
{
$this->castle = new Castle();
}
public function createWall(): void
{
$this->castle->wall = 32;
}
public function createRoof(): void
{
$this->castle->roof = "Done!";
}
public function createDoor(): void
{
$this->castle->door = 8;
}
public function createWindow(): void
{
$this->castle->window = 16;
}
public function createGarage(): void
{
$this->castle->garage = true;
}
public function createSwimmingPool(): void
{
$this->castle->swimmingPool = true;
}
public function getResult(): Castle
{
$result = $this->castle;
$this->reset();
return $result;
}
}
/**
* This is a Director Class which its execute the building steps in particular sequence
* 2nees.com
*/
class Director
{
private HouseBuilderInterface $builder;
public function setBuilder(HouseBuilderInterface $builder): void
{
$this->builder = $builder;
}
public function buildYourHouse(): void
{
$this->builder->createWall();
$this->builder->createRoof();
$this->builder->createDoor();
$this->builder->createWindow();
$this->builder->createGarage();
$this->builder->createSwimmingPool();
}
}
/**
* Client code, simulate to build castel and home
* 2nees.com
*/
$director = new Director();
$homeBuilder = new HouseBuilder();
$director->setBuilder($homeBuilder);
$director->buildYourHouse();
$home = $homeBuilder->getResult();
print_r($home);
$home->buildHome();
echo "=========================================" . PHP_EOL;
$castleBuilder = new CastleBuilder();
$director->setBuilder($castleBuilder);
$director->buildYourHouse();
$castle = $castleBuilder->getResult();
print_r($castle);
$castle->buildHome();