php

<?php
/**
* @Description: 桥接模式
* @Author: luoxiaojin
* @Date: 2020-06-30 10:06:41
* @LastEditors: luoxiaojin
* @LastEditTime: 2020-06-30 11:00:04
* @FilePath: design_patternsl10.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();

  

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