设计模式-桥
作者:edwin
日期:2019-08-05 15:00:31
所属分类:后端 - php

分离这个模式将从其实现中分离出一个抽象以便这两个可以单独改变

ConcreteImplementorB(具体实现B)
This class implements the Implementor interface and defines its concrete implementation.
(这个类实现实现者的接口和定义其具体实现。)

ConcreteImplementorA(具体实现A)
This class implements the Implementor interface and defines its concrete implementation.
(这个类实现实现者的接口和定义其具体实现。)

RefinedAbstraction(精确抽像接口)
This class extends the interface defined by Abstraction.
(这个类扩展由抽象定义的接口。)

Implementor(实现)
This class defines the interface for implementation classes.
(这个类定义了实现类的接口。)

Abstraction(抽像)
This class (a) defines the abstraction's interface, and (b) maintains a reference to an object of type Implementor.
(这类(a)定义了抽象的接口,和(b)维护一个实现者类型的对象的引用。)

代码实现

<?php

class ConcreteImplementorA extends Implementor
{
    public function operation() {
        echo "ConcreteImplementorA Operation";
    }
}


class ConcreteImplementorB extends Implementor
{
    public function operation() {
        echo "ConcreteImplementorB Operation";
    }
}

class RefinedAbstraction extends Abstraction
{
}


class ExampleAbstraction extends Abstraction
{
}

abstract class Implementor {
    abstract public function operation();
}

class Abstraction
{
    protected $_implementor = null;

    public function setImplementor($implementor) {
        $this->_implementor = $implementor;
    }

    public function operation() {
        $this->_implementor->operation();
    }
}

$objRAbstraction = new RefinedAbstraction();
$objRAbstraction->setImplementor(new ConcreteImplementorB());
$objRAbstraction->operation();

$objRAbstraction->setImplementor(new ConcreteImplementorA());
$objRAbstraction->operation();

$objEAbstraction = new ExampleAbstraction();
$objEAbstraction->setImplementor(new ConcreteImplementorB());
$objEAbstraction->operation();

评论

全部评论 / 0