PHP桥接模式Bridge Pattern的优点与实现过程(桥接SFU)新鲜出炉

随心笔谈12个月前发布 admin
63 0

<?php
// 实现类接口
interface Implementor
{
public function operationImpl();
}
// 具体实现类A
class ConcreteImplementorA implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorA operation.”;
}
}
// 具体实现类B
class ConcreteImplementorB implements Implementor
{
public function operationImpl()
{
return “ConcreteImplementorB operation.”;
}
}
// 抽象类
abstract class Abstraction
{
protected $implementor;
public function __construct(Implementor $implementor)
{
$this->implementor=$implementor;
}
abstract public function operation();
}
// 扩展抽象类
class RefinedAbstraction extends Abstraction
{
public function operation()
{
return $this->implementor->operationImpl();
}
}
// 客户端代码
$implementorA=new ConcreteImplementorA();
$abstraction=new RefinedAbstraction($implementorA);
echo $abstraction->operation(); // 输出 “ConcreteImplementorA operation.”

© 版权声明

相关文章