php设计模式 Bridge (桥接模式)

Badia ·
更新时间:2024-09-21
· 755 次阅读

代码如下:
<?php
/**
* 桥接模式
*
* 将抽象部份与它实现部分分离,使用它们都可以有独立的变化
*/
abstract class Implementor
{
abstract public function operation();
}
class ConcreteImplementorA extends Implementor
{
public function operation()
{
echo "ConcreteImplementorA Operation<br/>";
}
}
class ConcreteImplementorB extends Implementor
{
public function operation()
{
echo "ConcreteImplementorB Operation<br/>";
}
}
class Abstraction
{
protected $_implementor = null;
public function setImplementor($implementor)
{
$this->_implementor = $implementor;
}
public function operation()
{
$this->_implementor->operation();
}
}
class RefinedAbstraction extends Abstraction
{
}
class ExampleAbstraction extends Abstraction
{
}
//
$objRAbstraction = new RefinedAbstraction();
$objRAbstraction->setImplementor(new ConcreteImplementorB());
$objRAbstraction->operation();
$objRAbstraction->setImplementor(new ConcreteImplementorA());
$objRAbstraction->operation();
$objEAbstraction = new ExampleAbstraction();
$objEAbstraction->setImplementor(new ConcreteImplementorB());
$objEAbstraction->operation();
您可能感兴趣的文章:学习php设计模式 php实现桥梁模式(bridge)php桥接模式应用案例分析PHP设计模式(五)适配器模式Adapter实例详解【结构型】PHP设计模式(四)原型模式Prototype实例详解【创建型】PHP设计模式(三)建造者模式Builder实例详解【创建型】PHP设计模式(一)工厂模式Factory实例详解【创建型】PHP设计模式概论【概念、分类、原则等】PHP设计模式之 策略模式Strategy详解【对象行为型】PHP设计模式(六)桥连模式Bridge实例详解【结构型】



桥接 桥接模式 PHP

需要 登录 后方可回复, 如果你还没有账号请 注册新账号