Blogger Information
Blog 11
fans 0
comment 0
visits 6607
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP设计模式学习笔记——桥接模式
青Blue的博客
Original
692 people have browsed it

实例

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

运行实例 »

点击 "运行实例" 按钮查看在线实例

——学习参考与 bilibili燕十八 面向对象与设计模式

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post