1 <?php
2
3 /**
4 * 需求就是我需要一件代步的工具,好让我不用自己跑过去,你帮我造一个吧
5 */
6
7 /**
8 * 监工,控制配件的装卸顺序,还有装卸时间
9 */
10 class Director
11 {
12 public function build(Builder $builder)
13 {
14 $builder->createTravelTool();
15 $builder->addWheel();
16 $builder->addDoor();
17 $builder->addEngine();
18
19 return $builder->getTravelTool();
20 }
21 }
22
23
24 /* 这些是具体的抽象类,一件物品一个类 */
25
26 /**
27 * 轮子
28 */
29 class Wheel
30 {
31 private $_name = "我是一个轮子";
32
33 public function __toString()
34 {
35 return "<br/>{$this->_name}<br/>";
36 }
37 }
38
39 /**
40 * 车门
41 */
42 class Door
43 {
44 private $_name = "我是一扇车门";
45
46 public function __toString()
47 {
48 return "<br/>{$this->_name}<br/>";
49 }
50 }
51
52 /**
53 * 引擎
54 */
55 class Engine
56 {
57 private $_name = "我是一个引擎";
58
59 public function __toString()
60 {
61 return "<br/>{$this->_name}<br/>";
62 }
63 }
64
65 /**
66 * 卡车
67 */
68 class Trunk extends TravelTool
69 {
70 public function __construct()
71 {
72 $this->_name = "我是一辆卡车";
73 }
74 }
75
76 /**
77 * 小轿车
78 */
79 class Car extends TravelTool
80 {
81 public function __construct()
82 {
83 $this->_name = "我是一辆小轿车";
84 }
85 }
86
87 /**
88 * 因为卡车和小轿车有共同的部分,可以集中在一起,所以需要一个抽象类来做共同的部分
89 */
90 abstract class TravelTool
91 {
92 protected $_name;
93
94 public function __toString()
95 {
96 return "<br/>{$this->_name}<br/>";
97 }
98
99 public function assemble($class, $positon='')
100 {
101 echo $class,"位置:".$positon."<br/>";
102 }
103 }
104
105
106
107 /* 这些是具体的建造者,可以理解为组装车间,而Director就是监工,Director需要车间才能完成组装 */
108
109 /**
110 * 车间, 里面说明这样的车间应该有什么东西
111 */
112 interface Builder
113 {
114 public function addWheel(); //这个车间要可以装卸轮子
115 public function addDoor(); //要可以装卸车门
116 public function addEngine(); //要可以装卸引擎
117 public function createTravelTool(); //要可以装卸基本的框架
118 public function getTravelTool(); //要能把装配好的工具运送出去
119 }
120
121 /**
122 * 卡车组装车间
123 */
124 class TruckBuilder implements Builder
125 {
126 private $travelTool;
127
128 public function addWheel()
129 {
130 $wheel = new Wheel();
131 $this->travelTool->assemble($wheel, 'left front');
132 $this->travelTool->assemble($wheel, 'left backend');
133 $this->travelTool->assemble($wheel, 'right front');
134 $this->travelTool->assemble($wheel, 'right backend');
135 }
136
137 public function addDoor()
138 {
139 $this->travelTool->assemble(new Door(), 'left front');
140 $this->travelTool->assemble(new Door(), 'left backend');
141 $this->travelTool->assemble(new Door(), 'right front');
142 $this->travelTool->assemble(new Door(), 'right backend');
143 }
144
145 public function addEngine()
146 {
147 $this->travelTool->assemble(new Engine());
148 }
149
150 public function createTravelTool()
151 {
152 $this->travelTool = new Trunk();
153 }
154
155 public function getTravelTool()
156 {
157 return $this->travelTool;
158 }
159 }
160
161 /**
162 * 小轿车组装车间
163 */
164 class CarBuilder implements Builder
165 {
166 private $travelTool;
167
168 public function addWheel()
169 {
170 $this->travelTool->assemble(new Wheel(), 'left front');
171 $this->travelTool->assemble(new Wheel(), 'left backend');
172 $this->travelTool->assemble(new Wheel(), 'right front');
173 $this->travelTool->assemble(new Wheel(), 'right backend');
174 }
175
176 public function addDoor()
177 {
178 $this->travelTool->assemble(new Door(), 'left front');
179 $this->travelTool->assemble(new Door(), 'left backend');
180 $this->travelTool->assemble(new Door(), 'right front');
181 $this->travelTool->assemble(new Door(), 'right backend');
182 }
183
184 public function addEngine()
185 {
186 $this->travelTool->assemble(new Engine());
187 }
188
189 public function createTravelTool()
190 {
191 $this->travelTool = new Car();
192 }
193
194 public function getTravelTool()
195 {
196 return $this->travelTool;
197 }
198 }
199
200
201
202
203
204 //test
205 $t = new TruckBuilder();
206 $c = new CarBuilder();
207 $d = new Director();
208
209 echo "Trunk Builder<br/>";
210 $trunk = $d->build($t);
211 echo $trunk;
212
213 echo "<br/><br/>Car Builder<br/>";
214 $car = $d->build($c);
215 echo $car;