桥接模式
来源:互联网 发布:ba无标度网络 编辑:程序博客网 时间:2024/06/05 02:40
<?php /**桥接模式用户之间发送信息,站内信息、短信、email、手机....*/interface msg{public function send($to, $content);}class zn implements msg {public function send($to, $content){echo "站内信", $to, "内容", $content;}}class email implements msg {public function send($to, $content){echo "email", $to, "内容", $content;}}class sms implements msg {public function send($to, $content){echo "sms", $to, "内容", $content;}}//内容也细分了普通,加急,特急//由于我们只能扩展,不能修改,所以再添加的类话就添加的太多了。一共9个/*class zncommon implements msgclass znwarm implements msgclass zndanger implements msg......*/<====================================================================================================================><?php //解决方案//设计模式的目的就是解耦,但是不能完美。所以会有桥接模式。适当耦合!1//如果按照上面的方式,需要创建 m*n 个类。下面只需要 m+n 个就行 。防止子类爆炸的情况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);return $this->send->send($to, $content);}}class commoninfo extends info {public function msg($content) {return $content.'<br />普通';}}class warninfo extends info {public function msg($content) {return $content.'<br />紧急';}}class dangerinfo extends info {public function msg($content) {return $content.'<br />特急';}}class zn {public function send($to, $content){return "站内信给".$to."<br />内容是:".$content;}}class email {public function send($to, $content){return "email".$to."<br />内容是:".$content;}}class sms {public function send($to, $content){return "email".$to."<br />内容是:".$content;}}//使用站内发普通信息$commoninfo = new commoninfo(new zn());echo $commoninfo->send('小东','水电费');echo "<hr />";//使用短信发特急信息$dangerinfo = new dangerinfo(new sms());echo $dangerinfo->send('上帝','保佑我,心想事成!');
0 0
- 设计模式:适配器模式,桥接模式
- 设计模式----桥接模式
- 设计模式---桥接模式
- 结构性模式-桥接模式
- 设计模式:桥接模式
- 设计模式 桥接模式
- 设计模式 - 桥接模式
- 设计模式--桥接模式
- 设计模式--桥接模式
- 设计模式 - 桥接模式
- 设计模式-桥接模式
- 设计模式 - 桥接模式
- 设计模式 - 桥接模式
- 【设计模式-桥接模式】
- 设计模式:桥接模式
- 【设计模式】----桥接模式
- 【设计模式】桥接模式
- 设计模式--桥接模式
- 2332
- tomcat服务器下使用nginx实现最简单的动静分离--解决css、js不加载的问题
- 有销售的店铺数、有库存的店铺数,铺货的店铺数-已计算的关键指标
- pwnable 笔记 Toddler's Bottle - uaf
- 记录idea tomcat控制台乱码问题
- 桥接模式
- UVA 10976Fractions Again?!
- Spring 学习(一)-----装配Bean
- Ambari学习14_升级ambari、HDP版本(ambari 2.1升级到2.4、HDP2.3升级到2.5)
- (七)shiro-Spring结合
- 策略模式
- android ANR产生原因和解决办法
- Eclipse调试:改变颜色, 背景与字体大小 和xml字体调整
- linuxep笔记20161116