码迷,mamicode.com
首页 > Web开发 > 详细

PHP设计模式——桥接模式

时间:2016-02-23 18:35:05      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

<?php

/*
 * 桥接模式
 * 使用发送器,将一个类对象传入另一个类作为属性,耦合M+N个类
 * 
 */

abstract class Info {

    protected $_send = NULL;

    public function __construct($send) {
        $this->_send = $send;
    }

    abstract function msg($content);

    public function send($to, $content) {
        $content = $this->msg($content);
        $this->_send->send($to, $content);
    }

}

class Email {

    public function send($to, $content) {
        echo "Email: From:$to Content:$content<br>";
    }

}
class Sms {

    public function send($to, $content) {
        echo "Sms: From:$to Content:$content<br>";
    }

}

class CommonBridge extends Info{
    public function msg($content) {
        return ‘CommonBridge>>‘.$content;
    }
}
class DangerBridge extends Info{
    public function msg($content) {
        return ‘DangerBridge>>‘.$content;
    }
}
//调用桥接
$email = new Email();
$CommonEmail  = new CommonBridge($email);
$CommonEmail->send(‘Tom‘,‘XXXXX‘);

$DangerSms  = new DangerBridge(new Sms());
$DangerSms->send(‘Lucy‘,‘OOOOOOO‘);

 

PHP设计模式——桥接模式

标签:

原文地址:http://www.cnblogs.com/tlxma/p/5210719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!