Repository navigation
Expand file tree
/
Copy pathProxy.php
More file actions
82 lines (68 loc) · 1.4 KB
/
Copy pathProxy.php
File metadata and controls
82 lines (68 loc) · 1.4 KB
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
<?php
class SchoolGirl
{
private $name;
function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
// 代理接口
interface GiveGift
{
public function GiveDolls();
public function GiveFlowers();
public function GiveChocolate();
}
// 代理实现送礼物接口
class Proxy implements GiveGift
{
protected $pursuit;
function __construct(SchoolGirl $girl)
{
$this->pursuit = new Pursuit($girl);
}
public function GiveDolls()
{
$this->pursuit->GiveDolls();
}
public function GiveFlowers()
{
$this->pursuit->GiveFlowers();
}
public function GiveChocolate()
{
$this->pursuit->GiveChocolate();
}
}
// 追求者类实现送礼物接口
class Pursuit implements GiveGift
{
protected $girl;
function __construct(SchoolGirl $girl)
{
$this->girl = $girl;
}
public function GiveDolls()
{
echo $this->girl->getName()." 送你娃娃\n";
}
public function GiveFlowers()
{
echo $this->girl->getName()." 送你花\n";
}
public function GiveChocolate()
{
echo $this->girl->getName()." 送你巧克力\n";
}
}
// 客户端代码
$girl = new SchoolGirl('李梅');
$proxy = new Proxy($girl);
$proxy->GiveDolls();
$proxy->GiveChocolate();
$proxy->GiveFlowers();