zoukankan      html  css  js  c++  java
  • 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();
    

      

  • 相关阅读:
    android焦点
    URI和URL的区别比较与理解
    Android Bundle类
    repo命令
    ubuntu adb找不到设备
    【python】-网络编程
    【python】-反射
    【python】-类的特殊成员方法
    【python】-7-面向对象的进阶
    【python】-多态
  • 原文地址:https://www.cnblogs.com/weixinsb/p/13223575.html
Copyright © 2011-2022 走看看