<?php
//防止子类爆炸
//论坛给用户发信息,可以是站内短信,email, 手机
interface msg
{
public function send($to, $content)
{
}
}
class zn implements msg
{
public function send($to, $content)
{
echo '站内信给'.$to.'内容是:', $content;
}
}
class email implements msg
{
public function send($to, $content)
{
echo 'email给'.$to.'内容是:', $content;
}
}
class sms implements msg
{
public function send($to, $content)
{
echo '短信给'.$to.'内容是:', $content;
}
}
//内容也分普通,加急和特级
/*
class zncommon extends msg
class znwarn extends msg
class zndange extends msg
class emailcommon extends msg
class emailwarn extends msg
class emaildange extends msg
class smscommon extends msg
class smswarn extends msg
class smsdange extends msg
*/
//信的发送方式是一个变量,信的紧急程度是一个变量,
//为了不修改父类,只好考虑两个因素的组合,不断产生新类
<?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 '<br>站内给' . $to . '内容是:' . $content;
}
}
class email
{
public function send($to, $content)
{
echo '<br>email给' . $to . '内容是:' . $content;
}
}
class sms
{
public function send($to, $content)
{
echo '<br>短信给' . $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;
}
}
//站内普通信息
$zn = new zn();
$conmoninfo = new commoninfo($zn);
$conmoninfo->send('小明', '吃饭了');
//手机短信加急信息
$sms = new sms();
$warninfo = new warninfo($sms);
$warninfo->send('小刚', '失火了');
//站内普通信息
$email = new email();
$dangerinfo = new dangerinfo($email);
$dangerinfo->send('小红', '跳楼了');
12.桥接模式
最新推荐文章于 2023-03-26 16:11:28 发布