温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

什么是php桥接模式

发布时间:2021-07-02 16:14:42 来源:亿速云 阅读:144 作者:chen 栏目:编程语言

本篇内容主要讲解“什么是php桥接模式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“什么是php桥接模式”吧!

说明

1、将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果。

2、结构分为Abstraction抽象类、RefindAbstraction被提炼的抽象类、Implementor实现类、ConcreteImplementor具体实现类 、Client客户端代码。

实例

/**
 * 颜色抽象类
 * Class Colour
 */
abstract class Colour
{
    /**
     * @return mixed
     */
    abstract public function run();
}
 
 
/**
 * 黑色
 * Class Black
 */
class Black extends Colour
{
    public function run()
    {
        // TODO: Implement run() method.
        return '黑色';
    }
}
 
 
/**
 * 白色
 * Class White
 */
class White extends Colour
{
    public function run()
    {
        // TODO: Implement run() method.
        return '白色';
    }
}
 
 
/**
 * 红色
 * Class Red
 */
class Red extends Colour
{
    public function run()
    {
        // TODO: Implement run() method.
        return '红色';
    }
}
 
 
/**
 * 形状抽象类
 * Class Shape
 */
abstract class Shape
{
    /**
     * 颜色
     * @var Colour
     */
    protected $colour;
 
 
    /**
     * Shape constructor.
     * @param Colour $colour
     */
    public function __construct(Colour $colour)
    {
        $this->colour = $colour;
    }
 
 
    /**
     * @return mixed
     */
    abstract public function operation();
}
 
 
/**
 * 圆形
 * Class Round
 */
class Round extends Shape
{
    /**
     * @return mixed|void
     */
    public function operation()
    {
        // TODO: Implement operation() method.
        echo $this->colour->run() . '圆形<br>';
    }
}
 
 
/**
 * 长方形
 * Class Rectangle
 */
class Rectangle extends Shape
{
    /**
     * @return mixed|void
     */
    public function operation()
    {
        // TODO: Implement operation() method.
        echo $this->colour->run() . '长方形<br>';
    }
}
 
 
/**
 * 正方形
 * Class Square
 */
class Square extends Shape
{
    /**
     * @return mixed|void
     */
    public function operation()
    {
        // TODO: Implement operation() method.
        echo $this->colour->run() . '正方形<br>';
    }
}
 
 
// 客户端代码
// 白色圆形
$whiteRound = new Round(new White());
$whiteRound->operation();
 
// 黑色正方形
$blackSquare = new Square(new Black());
$blackSquare->operation();
 
// 红色长方形
$redRectangle = new Rectangle(new Red());
$redRectangle->operation();
 
 
// 运行结果
白色圆形
黑色正方形
红色长方形

到此,相信大家对“什么是php桥接模式”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI