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();
复制代码 代码如下:
<?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();