场景:上图会产生9种类型的信息,按一般的程序设计,需要写9个的信息类,但运用桥接设计模式只需要写M+N个类就可以实现
<?php
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 '手机短信发送给' . $to . '内容是' . $content;
}
}
//普通程度信息
class commoninfo extends info {
public function msg($content) {
return '普通' . $content;
}
}
//紧急程度信息
class warninfo extends info {
public function msg($content) {
return '紧急' . $content;
}
}
//特急程度信息
class dangerinfo extends info {
public function msg($content) {
return '特急' . $content;
}
}
//用站内发送普通信息
$commoninfo = new commoninfo(new zn());
$commoninfo->send('小明', '吃饭了');//站内给小明内容是普通吃饭了