欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-27 17:42:16
复制代码 代码如下:
复制代码 代码如下:

<?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();