-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathComposite.php
executable file
·488 lines (441 loc) · 13.6 KB
/
Composite.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/**
* 组合模式,其实就是数据结构里的数结构
*/
/**
* 定义一个根节点,就为总经理服务
*/
// interface IRoot {
//
// /* 得到总经理信息 */
// public function getInfo();
//
// /* 总经理下边还有虾兵蟹将,那得能增加虾兵蟹将, 比如研发部经理,这是个树枝节点 */
// public function addBrance($brance);
//
// /* 还要能增加叶子节点 */
// public function addLeaf($leaf);
//
// /* 既然增加了那得能遍历,不可能总经理不知道他手下的那些人吧 */
// public function getSubordinateInfo();
//
// }
/**
* 根节点实现类
*/
// class Root implements IRoot {
//
// private $subordinateList = array(); //保存根节点下的分支和树叶
//
// private $name = ''; //根节点名称
//
// private $position = ''; //根节点的职位
//
// private $salary = 0; //根节点薪水
//
// /* 根据构造函数传递进来总经理信息 */
// public function __construct($name, $position, $salary) {
// $this->name = $name;
// $this->position = $position;
// $this->salary = $salary;
//
// }
//
// /* 增加树枝检点 */
// public function addBrance($brance) {
// array_push($this->subordinateList, $brance);
// }
//
// /* 增加叶子节点 */
// public function addLeaf($leaf) {
// array_push($this->subordinateList, $leaf);
// }
//
// /* 得到自己的信息 */
// public function getInfo() {
// $info = '';
// $info .= '名称:'.$this->name;
// $info .= "\t职位: ".$this->position;
// $info .= "\t薪水:".$this->salary;
// return $info;
// }
//
// /* 得到下级信息 */
// public function getSubordinateInfo() {
// return $this->subordinateList;
// }
//
// }
/**
* 树枝节点,也就是各个部门经理和组长的角色
*/
// interface IBrance {
//
// /* 获得信息 */
// public function getInfo();
//
// /* 增加数据节点,例如研发不下的研发一组 */
// public function addBrance($brance);
//
// /* 增加叶子节点 */
// public function addLeaf($leaf);
//
// /* 获得下级信息 */
// public function getSubordinateInfo();
// }
/**
* 所有树枝节点的实现
*/
// class Brance implements IBrance {
//
// private $subordinateList = array(); //存储子节点信息
//
// private $name = ''; //树枝节点名称
//
// private $position = ''; //树枝节点职位
//
// private $salary = 0; //树枝节点薪水
//
// public function __construct($name, $position, $salary) {
// $this->name = $name;
// $this->position = $position;
// $this->salary = $salary;
// }
//
// public function addBrance($brance) {
// array_push($this->subordinateList, $brance);
// }
//
// public function addLeaf($leaf) {
// array_push($this->subordinateList, $leaf);
// }
//
// /* 得到自己的信息 */
// public function getInfo() {
// $info = '';
// $info .= '名称:'.$this->name;
// $info .= "\t职位: ".$this->position;
// $info .= "\t薪水:".$this->salary;
// return $info;
// }
//
// /* 得到下级信息 */
// public function getSubordinateInfo() {
// return $this->subordinateList;
// }
// }
/**
* 叶子节点,也就是最小的小兵了,
*/
// interface ILeaf {
//
// /* 获得自己信息 */
// public function getInfo();
//
// }
/**
* 最小的叶子节点
*/
// class Leaf implements ILeaf {
//
// private $name = ''; //叶子节点叫什么名字
//
// private $position = ''; //叶子节点的职位是什么
//
// private $salary = 0; //薪水
//
// public function __construct($name, $position, $salary) {
// $this->name = $name;
// $this->position = $position;
// $this->salary = $salary;
// }
//
// /* 得到自己的信息 */
// public function getInfo() {
// $info = '';
// $info .= '名称:'.$this->name;
// $info .= "\t职位: ".$this->position;
// $info .= "\t薪水:".$this->salary;
// return $info;
// }
// }
//
// /* 组装一个树,并遍历一遍 */
// $ceo = new Root('王大麻子', '1经理', 100000); //根节点ceo
//
// /* 产生三个部门经理 */
// $developDep = new Brance('刘大瘸子', '2研发部经理', 10000);
// $salasDep = new Brance('马二拐子', '2销售部经理', 20000);
// $financeDep = new Brance('赵三驼子', '2财务不经理', 30000);
//
// /* 产生三个小组长 */
// $firstDevGroup = new Brance('杨三乜斜', '3开发一组组长', 5000);
// $secondDevGroup = new Brance('吴大棒槌', '3开发二组组长',6000);
//
// /* 剩下的及时我们这些小兵了,就是路人甲,路人乙 */
//
// $a = new Leaf("a", "4开发人员", 2000);
// $b = new Leaf("b", "4开发人员", 2000);
// $c = new Leaf("c", "4开发人员", 2000);
// $d = new Leaf("d", "4开发人员", 2000);
// $e = new Leaf("e", "4开发人员", 2000);
// $f = new Leaf("f", "4开发人员", 2000);
// $g = new Leaf("g", "开发人员", 2000);
// $h = new Leaf("h", "3销售人员", 5000);
// $i = new Leaf("i", "3销售人员", 4000);
// $j = new Leaf("j", "3财务人员", 5000);
// $k = new Leaf("k", "2CEO秘书", 8000);
//
// $zhengLaoLiu = new Leaf("郑老六", "3研发部副总", 20000);
//
// /* 首先是定义总经理下有三个部门经理 */
// $ceo->addBrance($developDep);
//
// $ceo->addBrance($salasDep);
//
// $ceo->addBrance($financeDep);
//
// //总经理下还有一个秘书
// $ceo->addLeaf($k);
//
// //定义研发部门 下的结构
// $developDep->addBrance($firstDevGroup);
//
// $developDep->addBrance($secondDevGroup);
//
// //研发部经理下还有一个副总
// $developDep->addLeaf($zhengLaoLiu);
//
// //看看开发两个开发小组下有什么
// $firstDevGroup->addLeaf($a);
//
// $firstDevGroup->addLeaf($b);
//
// $firstDevGroup->addLeaf($c);
//
// $secondDevGroup->addLeaf($d);
//
// $secondDevGroup->addLeaf($e);
//
// $secondDevGroup->addLeaf($f);
//
// //再看销售部下的人员情况
// $salasDep->addLeaf($h);
//
// $salasDep->addLeaf($i);
//
// //最后一个财务
// $financeDep->addLeaf($j);
//
// //打印出来整个树形
// $tree = $ceo->getSubordinateInfo();
//
// readTree($ceo);
// /* 开始设计模式 */
// interface ICorp {
//
// /* 每个员工都有信息,你想隐藏,门都没有啊 */
// public function getInfo();
//
// }
//
// /**
// * 树叶节点,即小兵
// */
// class Leaf implements ICorp {
//
// /* 小兵也有名字 */
// private $name = '';
//
// /* 小兵也有职位 */
// private $position = '';
//
// /* 小兵也有薪水,否则谁给你干 */
// private $salary = 0;
//
// /* 构造函数传递小兵信息 */
// public function __construct($name, $position, $salary) {
// $this->name = $name;
// $this->position = $position;
// $this->salary = $salary;
// }
//
// /* 获得小兵系想你 */
// public function getInfo() {
// $info = '';
// $info .= '名称:'.$this->name;
// $info .= "\t职位: ".$this->position;
// $info .= "\t薪水:".$this->salary;
// return $info;
//
// }
// }
//
// /**
// * 树枝节点接口,即经理级别的人
// */
//
// interface IBrance {
//
// /* 增加小兵(树叶节点)或经理级别的人(树枝节点) */
// public function addSubordinate($corp);
//
// /* 还能够获得下属信息 */
// public function getSubordinateInfo();
// }
//
// /**
// * 树枝节点特点,就是领导既要知道自己的信息,也要知道下属的信息
// *
// */
// class Brance implements ICorp,IBrance {
//
// /* 领导需要知道自己的名字 */
// private $name = '';
//
// /* 领导的职位 */
// private $position = '';
//
// /* 领导的薪水 */
// private $salary = 0;
//
// /* 领导下边的虾兵蟹将信息 */
// private $subordinateList = array();
//
// public function __construct($name, $position, $salary) {
// $this->name = $name;
// $this->position = $position;
// $this->salary = $salary;
// }
//
// public function addSubordinate($corp) {
// array_push($this->subordinateList, $corp);
// }
//
// public function getSubordinateInfo() {
// return $this->subordinateList;
// }
//
// public function getInfo() {
// $info = '';
// $info .= '名称:'.$this->name;
// $info .= "\t职位: ".$this->position;
// $info .= "\t薪水:".$this->salary;
// return $info;
// }
// }
/* 继续优化,返现Brance和Leaf都有getinfo() ,继续抽象一下*/
abstract class Corp {
/* 公司每个人的名字 */
private $name = '';
/* 公司每个人的职位 */
private $position = '';
/* 公司每个人的薪水 */
private $salary = 0;
/* 父节点 */
private $_parent = null;
public function __construct($name, $position, $salary) {
$this->name = $name;
$this->position = $position;
$this->salary = $salary;
}
public function getInfo() {
$info = '';
$info .= '名称:'.$this->name;
$info .= "\t职位: ".$this->position;
$info .= "\t薪水:".$this->salary;
return $info;
}
public function setParent($corp) {
$this->_parent = $corp;
}
public function getParentInfo() {
return $this->_parent;
}
}
/**
* 叶子节点,即普通员工,写一个构造函数就可以了啊
*/
class Leaf extends Corp {
public function __construct($name, $position, $salary) {
parent::__construct($name, $position, $salary);
}
}
class Brance extends Corp {
/* 树枝下的树枝或树叶信息 */
private $subordinateList = array();
public function __construct($name, $position, $salary) {
parent::__construct($name ,$position, $salary);
}
/* 增加下属 */
public function addSubordinate($corp) {
array_push($this->subordinateList, $corp);
}
/* 得到下属信息*/
public function getSubordinateInfo() {
return $this->subordinateList;
}
}
function readTree($obj) {
if (is_object($obj)) {
echo $obj->getInfo()."<br />";
if(method_exists($obj, 'getSubordinateInfo')) {
$son = $obj->getSubordinateInfo();
$cnt = count($son);
if($cnt > 0) {
foreach($son as $val) {
readTree($val);
}
}
}
}
}
/* 组装这个结构 */
$ceo = new Brance('王大麻子', '1经理', 100000); //根节点ceo
/* 产生三个部门经理 */
$developDep = new Brance('刘大瘸子', '2研发部经理', 10000);
$salasDep = new Brance('马二拐子', '2销售部经理', 20000);
$financeDep = new Brance('赵三驼子', '2财务不经理', 30000);
/* 产生三个小组长 */
$firstDevGroup = new Brance('杨三乜斜', '3开发一组组长', 5000);
$secondDevGroup = new Brance('吴大棒槌', '3开发二组组长',6000);
/* 剩下的及时我们这些小兵了,就是路人甲,路人乙 */
$a = new Leaf("a", "4开发人员", 2000);
$b = new Leaf("b", "4开发人员", 2000);
$c = new Leaf("c", "4开发人员", 2000);
$d = new Leaf("d", "4开发人员", 2000);
$e = new Leaf("e", "4开发人员", 2000);
$f = new Leaf("f", "4开发人员", 2000);
$g = new Leaf("g", "开发人员", 2000);
$h = new Leaf("h", "3销售人员", 5000);
$i = new Leaf("i", "3销售人员", 4000);
$j = new Leaf("j", "3财务人员", 5000);
$k = new Leaf("k", "2CEO秘书", 8000);
$zhengLaoLiu = new Leaf("郑老六", "3研发部副总", 20000);
/* 首先是定义总经理下有三个部门经理 */
$ceo->addSubordinate($developDep);
$ceo->addSubordinate($salasDep);
$ceo->addSubordinate($financeDep);
//总经理下还有一个秘书
$ceo->addSubordinate($k);
//定义研发部门 下的结构
$developDep->addSubordinate($firstDevGroup);
$developDep->addSubordinate($secondDevGroup);
//研发部经理下还有一个副总
$developDep->addSubordinate($zhengLaoLiu);
//看看开发两个开发小组下有什么
$firstDevGroup->addSubordinate($a);
$firstDevGroup->addSubordinate($b);
$firstDevGroup->addSubordinate($c);
$secondDevGroup->addSubordinate($d);
$secondDevGroup->addSubordinate($e);
$secondDevGroup->addSubordinate($f);
//再看销售部下的人员情况
$salasDep->addSubordinate($h);
$salasDep->addSubordinate($i);
//最后一个财务
$financeDep->addSubordinate($j);
//打印出来整个树形
$tree = $ceo->getSubordinateInfo();
readTree($ceo);
?>