<?php
#Bridge(桥接)模式:将抽象部分与它的实现部分分离,使它们都可以独立的变化
//实现站内、eamil都能发送普通信息和紧急信息
//抽象类,定义信息
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){
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("小明","吃饭了");
echo "<br>";
$dangerinfo = new dangerinfo(new sms());
$dangerinfo->send("小刚","失火了,快回家");
优点
分离接口及其实现部分, 将Abstraction与Implementor分享有助于降低对实现部分编译时刻的依赖性, 接口与实现分享有助于分层,从而产生更好的结构化系统
提高可扩充性
实现细节对客户透明。