php - 桥接模式

<?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();

  

posted @ 2020-07-02 11:05  kakasaber  阅读(101)  评论(0编辑  收藏  举报