10.php桥接模式

来源:互联网 发布:ck解析源码 编辑:程序博客网 时间:2024/05/21 08:57

    当遇到多种不同的处理组合时,为防止对象的爆炸而使用的设计模式。由一个类别对象做最后的发送,而其他类别做各种数据的处理。

<?phpabstract  class Info{    //创建的对象用于保存发送对象    public $obj;        function __construct(Type $obj){        $this->obj = $obj;     }        //用于调用组装各种信息的紧急程度    abstract function msg($message);        //用于调用后直接发送数据    function send($to, $content){        //先组装信息的紧急程度        $content = $this->msg($content);        //调用传递来的对象方法        return $this->obj->send($to, $content);    }    }/*  * 普通信息 *  */class Normal extends Info{    function msg($message){        return "普通信息".$message;    }}/*  * 加急信息 *  */class Urgent extends Info{    function msg($message){        return "紧急信息".$message;    }}interface Type{    function send($to, $content);}/*  * 站内信的方式进行数据发送 *  */class ZN implements Type{    function send($to, $content){        return "站内信,发送到".$to.",".$content;    }}/*  * 手机短信的方式发送数据 *  */class DX implements Type{    function send($to, $content){        return "手机短信,发送到".$to.",".$content;    }}$obj = new Urgent(new ZN());echo $obj->send('xin', "测试信息"); 




0 0
原创粉丝点击