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

php桥接模式(bridge pattern)

时间:2019-07-13 20:12:27      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:color   rac   change   bst   inf   action   interface   imp   protected   

有点通了

<?php
/*
The bridge pattern is used when we want to decouple a class or abstraction from its
implementation, allowing them both to change independently. This is useful when
the class and its implementation vary often
*/

interface MessagingInterface {
    public function send($body);
}

class TextMessage implements MessagingInterface {
    public function send($body) {
        echo "TextMessage > send > $body: <br/>";
    }
}

class HtmlMessage implements MessagingInterface {
    public function send($body) {
        echo "HtmlMessage > send > $body: <br/>";
    }
}

interface MailerInterface {
    public function setSender(MessagingInterface $sender);
    public function send($body);
}

abstract class Mailer implements MailerInterface {
    protected $sender;
    public function setSender(MessagingInterface $sender) {
        $this->sender = $sender;
    }
}

class PHPMailer extends Mailer {
    public function send($body) {
        $body .= "\n\n Set from a phpmailer .";
        return $this->sender->send($body);
    }
}

class SwiftMailer extends Mailer {
    public function send($body) {
        $body .= "\n\n Set from a swiftmailer .";
        return $this->sender->send($body);
    }
}

$phpMailer = new PHPMailer();
$phpMailer->setSender(new TextMessage());
$phpMailer->send(‘Hi!‘);

$swiftMailer = new SwiftMailer();
$swiftMailer->setSender(new HtmlMessage());
$swiftMailer->send(‘Hello!‘);
?>

技术图片

php桥接模式(bridge pattern)

标签:color   rac   change   bst   inf   action   interface   imp   protected   

原文地址:https://www.cnblogs.com/aguncn/p/11181779.html

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