我需要从一组实体中删除一些实体,为此,我一个接一个地调用4个服务,以删除与某些过滤器不匹配的实体。
class MainService
{
public function removeEntities($collection)
{
$collection = $this->subServiceA->removeEntities($collection);
$collection = $this->subServiceB->removeEntities($collection);
$collection = $this->subServiceC->removeEntities($collection);
$collection = $this->subServiceD->removeEntities($collection);
return $collection;
}
}
我认为装饰者,责任链或观察员可以处理此案,但我不知道哪个人最适合。
答案 0 :(得分:2)
一种简单的队列样式方法应该可以解决此问题。
interface IEntityWorker {
function process($item);
}
class Queue {
protected $workers = [];
public function registerWorker(IEntityWorker $worker) {
$this->workers[] = $worker;
}
public function process($item) {
foreach ($this->workers as $worker) {
$item = $worker->process($item);
}
return $item;
}
}