<?php
/**
* 桥接模式, 完成发送信息功能
* 需求例子:发送的信息类型有(站内,email,手机短信),信息的内容类型有(特急,紧急,普通),如果按设计模式为了解耦,子类将会很多
* 使用桥接模式,做适当的耦合完成功能
*
*/
/**
* 不同信息内容继承此类
*
*/
abstract class info{
/**
* 发送器
* @var Object
*/
protected $send = null;
/**
* 初始化时给发送器赋值(就是把发送方式的对象,赋值给$send)
* @param unknown $send
*/
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;
}
}
// 用站内发普通信息
$warnInfo = new WarnInfo(new sms());
$warnInfo->send('小明', '吃饭了');