方法1:
作者的原话:
想要实现插件方式,可以建立接口!使用接口的好处是可以将定义和实现隔离,另外就是实现多态。我们建立一个留言扩展接口ILWordExtension,该接口有两个函数beforeAppend和behindAppend。权限校验、内容检查、加分这些功能可以看作是实现ILWordExtension接口的三个实现类,主业务逻辑就依次遍历这三个实现类,来完成次业务逻辑。如图所示:
他的源码:
// 扩展接口
interface ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord);
// 添加留言后
public function behindAppend($newLWord);
};
// 检查权限
class CheckPowerExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
// 在这里判断用户权限
}
// 添加留言后
public function behindAppend($newLWord) {
}
};
// 检查留言文本
class CheckContentExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
if (stristr($newLWord, "SB")) {
throw new Exception();
}
}
// 添加留言后
public function behindAppend($newLWord) {
}
};
// 用户积分
class AddScoreExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
}
// 添加留言后
public function behindAppend($newLWord) {
// 在这里给用户积分
}
};
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
// 添加留言前
$this->beforeAppend($newLWord);
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
// 添加留言后
$this->behindAppend($newLWord);
}
// 添加留言前
private function beforeAppend($newLWord) {
// 获取扩展数组
$extArray = $this->getExtArray();
foreach ($extArray as $ext) {
// 遍历每一个扩展, 并调用其 beforeAppend 函数
$ext->beforeAppend($newLWord);
}
}
// 添加留言后
private function behindAppend($newLWord) {
// 获取扩展数组
$extArray = $this->getExtArray();
foreach ($extArray as $ext) {
// 遍历每一个扩展, 并调用其 behindAppend 函数
$ext->behindAppend($newLWord);
}
}
// 获取扩展数组,
// 该函数的返回值实际上是 ILWordExtension 接口数组
private function getExtArray() {
return array(
// 检查权限
new CheckPowerExtension(),
// 检查内容
new CheckContentExtension(),
// 加分
new AddScoreExtension(),
);
}
};
如果还有新需求,,我们只要再添加ILWordExtension 实现类并且把它注册到getExtArray函数里即可。程序从此有了条理,并且算是具备了可扩展性。
方法2:
使用扩展工厂类的好处就是可以随意的添加和移除扩展实例,这就很好的实现了可插入式编程。对于LWordServiceCore类只知道一个ILWordExtension接口,对于LWordExtensionFamily知道需要一一调用每个扩展,但是具体会有多少个扩展是通过MyExtensionFactory给出的。各负其责结构也很清晰。如果我们做一个假设,MyExtensionFactory类的createLWordExtension函数不是通过new关键字这样的硬编码方式来添加扩展列表,而是通过更巧妙的读取配置文件的方式来得到扩展列表,那么是不是更方便更灵活呢?不过这个就不再本文中讨论了。
中间服务层通过工厂类取得一个ILWordExtension接口的具体实例,然后调用其beforeAppend和behindAppend方法。当然中间服务并不知道工厂类返回的其实是一个含有多个ILWordExtension实例的容器(因为这个容器也实现了ILWordExtension接口),所以中间服务也就不知道扩展是被一一调用的。完整代码如代码10所示:
// 完整代码
// 扩展接口
interface ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord);
// 添加留言后
public function behindAppend($newLWord);
};
// 检查权限
class CheckPowerExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
// 在这里判断用户权限
}
// 添加留言后
public function behindAppend($newLWord) {
}
};
// 检查留言文本
class CheckContentExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
if (stristr($newLWord, "fuck"))
throw new Exception();
}
// 添加留言后
public function behindAppend($newLWord) {
}
};
// 用户积分
class AddScoreExtension implements ILWordExtension {
// 添加留言前
public function beforeAppend($newLWord) {
}
// 添加留言后
public function behindAppend($newLWord) {
// 在这里给用户积分
}
};
// 扩展家族
class LWordExtensionFamily implements ILWordExtension {
// 扩展数组
private $_extensionArray = array();
// 添加扩展
public function addExtension(ILWordExtension $extension) {
$this->_extensionArray []= $extension;
}
// 添加留言前
public function beforeAppend($newLWord) {
foreach ($this->_extensionArray as $extension) {
$extension->beforeAppend($newLWord);
}
}
// 添加留言后
public function behindAppend($newLWord) {
foreach ($this->_extensionArray as $extension) {
$extension->behindAppend($newLWord);
}
}
}
// 自定义扩展工厂
class MyExtensionFactory {
// 创建留言扩展
public static function createLWordExtension() {
$lwef = new LWordExtensionFamily();
// 添加扩展
$lwef->addExtension(new CheckPowerExtension());
$lwef->addExtension(new CheckLWordExtension());
$lwef->addExtension(new AddScoreExtension());
return $lwef;
}
}
// 中间服务层
class LWordServiceCore implements ILWordService {
// 添加留言
public function append($newLWord) {
// 获取扩展
$ext = MyExtensionFactory::createLWordExtension();
$ext->beforeAppend($newLWord);
// 调用数据访问层
$dbTask = new LWordDBTask();
$dbTask->append($newLWord);
$ext->behindAppend($newLWord);
}
};
从代码中可以看出虽然CheckPowerExtension、CheckContentExtension、AddScoreExtension以及LWordExtensionFamily都实现了ILWordExtension接口,但是它们的beforeAppend和behindAppend函数过程却完全不同!特别是LWordExtensionFamily扩展家族类,它并没有实质的业务逻辑处理过程,而是将调用依次传递给每一个扩展。beforeAppend和behindAppend函数在具体类中的不同实现,这是面向对象程序设计中的很典型的特性:多态
上一篇:overflow属性 下一篇:快递查询API