-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.php
93 lines (72 loc) · 1.63 KB
/
builder.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
<?php
/**
* 建造者模式
*/
class Person {
private $head;
private $body;
private $foot;
//头
public function getHead() {
return $this->head;
}
public function setHead($head) {
$this->head = $head;
}
//体
public function getBody() {
return $this->body;
}
public function setBody($body) {
$this->body = $body;
}
//脚
public function getFoot() {
return $this->foot;
}
public function setFoot($foot) {
$this->foot = $foot;
}
}
//创建一个product对象的各个部件指定抽象接口
interface PersonBuilder {
public function buildHead();
public function buildBody();
public function buildFoot();
//产品返还方法
public function getResult();
}
//实现接口
class ConcreteBuilder implements PersonBuilder {
private $person;
function __construct() {
$this->person = new Person();
}
function buildHead() {
$this->person->setHead('建造头 ~');
}
function buildBody() {
$this->person->setBody('建造身体 ~');
}
function buildFoot() {
$this->person->setFoot('建造脚 ~');
}
function getResult() {
return $this->person;
}
}
//指导者
class Director {
public function __construct(ConcreteBuilder $builder) {
$builder->buildHead();
$builder->buildBody();
$builder->buildFoot();
}
}
//Test
$builder = new ConcreteBuilder();
$director = new Director($builder);
$person = $builder->getResult();
echo $person->getHead();
echo $person->getBody();
echo $person->getFoot();