zoukankan      html  css  js  c++  java
  • 桥接模式

    <?php
    #Bridge(桥接)模式:将抽象部分与它的实现部分分离,使它们都可以独立的变化
    
    
    
    //实现站内、eamil都能发送普通信息和紧急信息 
    //抽象类,定义信息 
    abstract class info{
        protected $send = null;
        public function __construct($send){
            $this->send = $send;
        }
        abstract public function msg($content);
        public function send($to,$content){
            $content = $this->msg($content);
            $this->send->send($to,$content);
        }
    }
    
    class zn{
        public function send($to,$content){
            echo "站内给",$to,"内容是:",$content;
        }
    }
    
    class email{
        public function send($to,$content){
            echo "email给",$to,"内容是:",$content;
        }
    }
    class sms{
        public function send($to,$content){
            echo "sms给",$to,"内容是:",$content;
        }
    }
    
    //扩展抽象类
    class commoninfo extends info{
        public function msg($content){
            return "普通".$content;
        }
    }
    class warninfo extends info{
        public function msg($content){
            return "紧急".$content;
        }
    }
    class dangerinfo extends info{
        public function msg($content){
            return "特级".$content;
        }
    }
    
    $commoninfo = new commoninfo(new zn());
    $commoninfo->send("小明","吃饭了");
    
    echo "<br>";
    
    $dangerinfo = new dangerinfo(new sms());
    $dangerinfo->send("小刚","失火了,快回家");
    
    
    
    
    优点
    分离接口及其实现部分, 将Abstraction与Implementor分享有助于降低对实现部分编译时刻的依赖性, 接口与实现分享有助于分层,从而产生更好的结构化系统
    提高可扩充性
    实现细节对客户透明。
    

      

  • 相关阅读:
    海量数据挖掘--DB优化篇
    BabyLinux制作过程详解
    C语言中的位域、字节序、比特序、大小端
    使用Busybox制作CRAMFS文件系统成功
    使用BusyBox制作linux根文件系统(CramFS+mdev)
    常用 U-boot命令详解
    基本C库函数
    simple_strtoul()
    在u-boot中添加命令
    U-Boot添加menu命令的方法及U-Boot命令执行过程
  • 原文地址:https://www.cnblogs.com/sunlong88/p/8691458.html
Copyright © 2011-2022 走看看