PHP桥接模式
PHP桥接模式
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
// 桥接模式 bridge

// 论坛给用户发消息,可以是站内短息,email,手机

abstract class info
{
protected $send = null;

public function __construct($send)
{
$this->send = $send;
}

abstract public function msg($content);

public function send($to, $content)
{
$content = $this->msg($content);
$this->send->send($to, $content);
}
}

class zn
{
public function send($to, $content)
{
echo ' 站内给 ', $to, ' 内容是: ', $content;
}
}

class email
{
public function send($to, $content)
{
echo ' email给 ', $to, ' 内容是: ', $content;
}
}

class sms
{
public function send($to, $content)
{
echo ' sms给 ', $to, ' 内容是: ', $content;
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class commonInfo extends info
{

public function msg($content)
{
// TODO: Implement msg() method.
return ' 普通 ' . $content;
}
}

class warnInfo extends info
{

public function msg($content)
{
// TODO: Implement msg() method.
return ' 紧急 ' . $content;
}
}

class dangerInfo extends info
{
public function msg($content)
{
// TODO: Implement msg() method.
return ' 特急 ' . $content;
}
}

// 用站内发普通消息
$commonInfo = new commonInfo(new zn());
$commonInfo->send('小飞飞', 'hello world');

echo '<br>';
// 用手机发特急信息
$dangerInfo = new commonInfo(new sms());
$dangerInfo->send('飞', '这是手机信息');