zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

PHP模式设计模式 桥接 Bridge
2023-06-13 09:14:29 时间
复制代码代码如下:

<?php
/**
*桥接模式
*
*将抽象部份与它实现部分分离,使用它们都可以有独立的变化
*/
abstractclassImplementor
{
abstractpublicfunctionoperation();
}
classConcreteImplementorAextendsImplementor
{
publicfunctionoperation()
{
echo"ConcreteImplementorAOperation<br/>";
}
}
classConcreteImplementorBextendsImplementor
{
publicfunctionoperation()
{
echo"ConcreteImplementorBOperation<br/>";
}
}
classAbstraction
{
protected$_implementor=null;
publicfunctionsetImplementor($implementor)
{
$this->_implementor=$implementor;
}
publicfunctionoperation()
{
$this->_implementor->operation();
}
}
classRefinedAbstractionextendsAbstraction
{
}
classExampleAbstractionextendsAbstraction
{
}
//
$objRAbstraction=newRefinedAbstraction();
$objRAbstraction->setImplementor(newConcreteImplementorB());
$objRAbstraction->operation();
$objRAbstraction->setImplementor(newConcreteImplementorA());
$objRAbstraction->operation();
$objEAbstraction=newExampleAbstraction();
$objEAbstraction->setImplementor(newConcreteImplementorB());
$objEAbstraction->operation();