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

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

<?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错误Cannot use object of type stdClass as array in错误的解决办法

很多人在PHP输出一个二维数组的时候出现“Fatal error: Cannot use object of type stdClass as array in……”。解决办法分析如下:...

重新封装zend_soap实现http连接安全认证的php代码

复制代码 代码如下: <?php class MyFramework_Soap_server extends Zend_Soap_Server { protected $_logi...

高质量PHP代码的50个实用技巧必备(上)

50个高质量PHP代码的实用技巧,希望大家喜欢。 1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该...

php 从指定数字中获取随机组合的简单方法(推荐)

例如:给定数字100,需要随机获取3个组成这个数字的组合,例如70,20,10 代码如下: <?php /** * 获取指定数字的随机数字组合 * @param I...

linux下编译安装memcached服务

linux下编译安装memcached服务

系统:Ubuntu 13.10 第一步:安装libevent-dev $aptitude search libevent-dev $aptitude install libevent...