PHP设计模式系列(十七):桥接模式

来源:互联网 发布:淘宝买手店 编辑:程序博客网 时间:2024/05/29 09:32

桥接模式

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

模式结构

桥接模式包含如下角色:

  • Abstraction:抽象类
  • RefinedAbstraction:扩充抽象类
  • Implementor:实现类接口
  • ConcreteImplementor:具体实现类

结构图

这里写图片描述

PHP代码实现

<?php/** * 桥接模式 *///Implementor:实现类接口abstract class Implementor{    abstract public function Operation();}//ConcreteImplementor:具体实现类class ConcreteImplementorA extends Implementor{    public function Operation(){        var_dump('A的方法执行');    }}class ConcreteImplementorB extends Implementor{    public function Operation(){        var_dump('B的方法执行');    }}//Abstraction:抽象类abstract class Abstraction{    abstract public function Operation();}//RefinedAbstraction:扩充抽象类class RefinedAbstraction extends Abstraction{    public function SetImplementor($implementor){        $this->implementor=$implementor;    }    public function Operation(){        $this->implementor->Operation();    }}$a=new RefinedAbstraction();$a->SetImplementor(new ConcreteImplementorA());$a->Operation();$a->SetImplementor(new ConcreteImplementorB());$a->Operation();

运行结果

string 'A的方法执行' (length=16)string 'B的方法执行' (length=16)
0 0
原创粉丝点击