2018-2-6-PHP设计模式【桥接模式】

layouttitledateauthordescin_head
post
PHP设计模式之桥接模式
2018-02-06 09:00:02 +0800
南丞
设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。设计模式于己于他人于系统都是多赢的;设计模式使代码编制真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
<style> .article-content p{ text-indent: 2em; line-height: 1.75rem; }</style>

桥接模式

<?php
    header("Content-type: text/html; charset=utf-8");
    require_once './vendor/autoload.php';
    //场景:将对象与类型解耦,对象与类型之间可以随意关联调用
    /**
     * 抽象 道路
     */
    abstract class Road
    {
        public $ICAR;
    
        abstract public function RoRun();
    }
    
    /**
     * 创建  告诉公路
     */
    class SpeedRoad extends Road
    {
        public function RoRun()
        {
            $this->ICAR->IcRun();
            echo '急速的行驶在高速公路上!<br>';
        }
    }
    
    /**
     * 创建 乡村小道
     */
    class StreetRoad extends Road
    {
        public function RoRun()
        {
            $this->ICAR->IcRun();
            echo '欢快的行驶在乡村小道上<br>';
        }
    }
    
    /**
     * 抽象接口  车
     */
    interface ICAR
    {
        function IcRun();
    }
    
    /**
     * 创建  轿车
     */
    class Car implements ICAR
    {
        public function IcRun()
        {
            echo '玛莎拉蒂';
        }
    }
    
    /**
     * 创建 马车
     */
    
    class Carriage implements ICAR
    {
        public function IcRun()
        {
            echo '马车';
        }
    }
    
    # 让轿车跑到 乡村小道
    $speedRoad=new StreetRoad();  // 先创建乡村小道
    $speedRoad->ICAR=new Car();   // 再把轿车放到马路上
    $speedRoad->RoRun();
    # 让马车跑到 高速公路
    $street=new SpeedRoad();      // 先创建高速公路
    $street->ICAR=new Carriage(); // 再把马车放到马路上
    $street->RoRun();
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值