bridge pattern 桥接模式 php

定义

将抽象部分与实现部分分离,使它们都可以独立地变化。是一种对象结构型模式。

角色

Abstraction抽象类

定义抽象类的接口,其中定义了一个Implement类型的对象并且可以维护该对象,与Implement之间具有关联关系,其可以包含抽象的业务方法,还可以包含具体的业务方法。

RefinedAbstraction扩充抽象类

扩充由Abstraction定义的接口,实现Abstraction中定义的抽象业务方法。

Implement实现类接口

定义实现类的接口,仅提供基本操作,仅定义基本操作,具体实现交给子类,通过有Abstraction的关联关系,在Abstraction中不仅拥有自己的方法,还可以调用Implement中定义的方法。

ConcreteImplementor具体实现类

实现implement接口并且具体实现它,在不同的ConcreteImplement中提供基本操作的不同实现。在程序运行时,ConcreteImplement对象将替代其父类对象,提供给客户端具体的业务操作方法。

Abstraction

// 抽象角色,给出定义,并保存一个对实现化对象的引用
abstract class Abstraction{
    protected $concreteImp;
    public function operation(){
        $this->concreteImp->operationImp();
    }
}

RefinedAbstraction

require_once "Abstraction.php";
// 扩充抽象角色
class RefinedAbstraction extends Abstraction{
    //可以将构造方法放到父类更好理解
    public function __construct(Implement $implement)
    {
        $this->concreteImp = $implement;
    }
    //扩展实现方法
    public function operation(){
        echo "扩展抽象类";
        $this->concreteImp->operationImp();//通过调用,可以将两个对象组合在一起
    }
}

Implementor

// 实现类角色,给出实现化角色的接口
abstract class Implement{
    // 操作方法的实现化声明
    abstract public function operationImp();
}

ConcreteImplementorA

require_once "Implement.php";
// 具体角色,给出实现化角色接口的具体实现
class ConcreteImplementA extends Implement{
    public function operationImp(){
        echo 'concrete implementA operation';
    }
}

ConcreteImplementorB

class ConcreteImplementB extends Implement{
    public function operationImp(){
        echo 'concrete implementB operation';
    }

}

 Client

require_once "RefinedAbstraction.php";
require_once "ConcreteImplementA.php";
$abstration = new RefinedAbstraction(new ConcreteImplementA);
$abstration->operation();

实例:

某软件公司要开发一个跨平台图像浏览系统,要求该系统能够显示BMP、JPG、GIF、PNG等多种格式的文件,并且能够在Windows、Linux、UNIX等多个操作系统上运行。系统首先将各种格式的文件解析为像素矩阵(Matrix),然后将像素矩阵显示在屏幕上,在不同的操作系统中可以调用不同的绘制函数来绘制像素矩阵(变化最大,可考虑成业务实现部分)。另外,系统需具有较好的扩展性,以便在将来支持新的文件格式和操作系统。试使用桥接模式设计该跨平台图像浏览系统。

代码

Client

require_once 'Windows.php';
require_once 'BMP.php';
$operation = new Windows(new BMP);
$operation->opeSys();

System

class System{
    private $imgtype;
    public function opeSys(){
        $this->imgtype->show();
    }
}

Windows

require_once 'System.php';
class Windows extends System 
{
    public function __construct(Imgtype $imgtype)
    {
        $this->imgtype = $imgtype;
    }
    public function opeSys(){
        echo "this is Windows show",PHP_EOL;
        $this->imgtype->show();
    }
}

Linux

class Linux extends System 
{
    public function __construct(Imgtype $imgtype)
    {
        $this->imgtype = $imgtype;
    }
    public function opeSys(){
        echo "this is Linux show",PHP_EOL;
        $this->imgtype->show();
    }
}

UNIX

class UNIX extends System 
{
    public function __construct(Imgtype $imgtype)
    {
        $this->imgtype = $imgtype;
    }
    public function opeSys(){
        echo "this is UNIX show",PHP_EOL;
        $this->imgtype->show();
    }
}

Imgtype

interface Imgtype{
    public function show();
}

BMP

require_once 'Imgtype.php';
class BMP implements Imgtype
{
    public function show(){
        echo "BMP";
    }
}

JPG

class JPG implements Imgtype
{
    public function show(){
        echo "JPG";
    }
}

GIF

class GIF implements Imgtype
{
    public function show(){
        echo "GIF";
    }
}

PNG

class PNG implements Imgtype
{
    public function show(){
        echo "PNG";
    }
}

参考:https://www.cnblogs.com/lmaster/p/7204928.html

https://www.cnblogs.com/yujon/p/5534793.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值