首页 > Web开发 > 详细

php - 桥接模式

时间:2020-07-02 12:06:26      阅读:56      评论:0      收藏:0      [点我收藏+]
<?php
/**
* @Description: 桥接模式
* @Author: luoxiaojin
* @Date: 2020-06-30 10:06:41
* @LastEditors: luoxiaojin
* @LastEditTime: 2020-06-30 11:00:04
* @FilePath: \design_patterns\l10.php
*/

// 论坛发送站内信

abstract class Msg{
    // 发送方式
    protected $send = null;
    // 发送内容
    protected $text = ‘‘;

    public function content(){
        // 
    }
    public function send(){
        // 
    }
}

// 发送方式
class EmailMsg extends Msg{
    public function __construct($to,$text){
        $this->text = $to.$text;
    }

    public function content(){
        return ‘Email:‘.$this->text;
    }
}

class SmsMsg extends Msg{
    public function __construct($to,$text){
        $this->text = $to.$text;
    }

    public function content(){
        return ‘短信:‘.$this->text;
    }
}

// 紧急程度
class CommonSend extends Msg{
    public function __construct(Msg $obj){
        $this->text = $obj->content();
    }

    public function send(){
        return "{$this->text},普通信息!";
    }
}

class UrgentSend extends Msg{
    public function __construct(Msg $obj){
        $this->text = $obj->content();
    }
    
    public function send(){
        return "{$this->text},紧急通知!";
    }
}

// 耦合调用
$commonEmailMsg = new CommonSend(new EmailMsg(‘小明‘,‘吃饭了!‘));
echo $commonEmailMsg->send();

$urgentSmsMsg = new UrgentSend(new SmsMsg(‘小红‘,‘家里失火了!‘));
echo $urgentSmsMsg->send();

  

php - 桥接模式

原文:https://www.cnblogs.com/weixinsb/p/13223575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!