我想知道是否有更好的方法来制动"在某些条件下的方法。让我用代码更好地解释一下:
function execute($context)
{
// some init actions
$event = new BeforeOperationOne();
$this->dispatch($event);
if ($event->accessGranted()) {
$context->setUser($this->user);
// other repeated code
return;
}
$result = $this->operationOne();
// some other code
$event = new BeforeOperationTwo();
$this->dispatch($event);
if ($event->accessGranted()) {
$context->setUser($this->user);
// other repeated code
return;
}
// this is not important what is access checker,
// this is just to show that all following code uses data
// computed in previous steps
$accessChecker = new AccessChecker($result);
$this->operationTwo(accessChecker);
// some other code
$event = new BeforeOperationThree();
$this->dispatch($event);
if ($event->accessGranted()) {
$context->setUser($this->user);
// other repeated code
return;
}
$this->operationThree();
// some other code
}
我们在这里重复了这个条件,当用户从事件访问时在上下文中设置用户。我能想到的选择是:
if (!$this->handleEvent($event, $context)) { return; }
- 这并没有多大帮助,也无法想到一个更好的名称句柄并不能说它返回的内容您对如何更好地编写它有什么想法吗?
答案 0 :(得分:1)
@Greg我在想这样的事情:
abstract class Handler
{
protected $nextHandler = null;
abstract public function Request($request);
public function setNextHandler(Handler $handler)
{
$this->nextHandler = $handler;
}
protected function someOperations($event)
{
//i copied this section, so you must shape that
$this->dispatch($event);
if ($event->accessGranted()) {
$context->setUser($this->user);
// other repeated code
return true;
}
return false;
}
}
class BeforeOperationOneHandler extends Handler
{
public function Request($request)
{
if ($this->someOperations(new BeforeOperationOne())) {
return;
}
$result = $this->operationOne(); // shape this too
return $this->nextHandler->Request($result);
}
}
class BeforeOperationTwoHandler extends Handler
{
public function Request($request)
{
if ($this->someOperations(new BeforeOperationTwo())) {
return;
}
$accessChecker = new AccessChecker($result); // shape this too
$result = $this->operationTwo(accessChecker);
return $this->nextHandler->Request($result);
}
}
class BeforeOperationThreeHandler extends Handler
{
public function Request($request)
{
if ($this->someOperations(new BeforeOperationThree())) {
return;
}
$result = $this->operationThree(); // shape this too
return $this->nextHandler->Request($result);
}
}
class DefaultHandler extends Handler
{
public function Request($request)
{
// this is the last step
}
}
function execute($context)
{
// some init actions
$beforeOperationOneHandler = new BeforeOperationOneHandler();
$beforeOperationTwoHandler = new BeforeOperationTwoHandler();
$beforeOperationThreeHandler = new BeforeOperationThreeHandler();
$defaultHandler = new DefaultHandler();
// set the sequence of the elements
// BeforeOperationOneHandler > BeforeOperationTwoHandler > BeforeOperationThreeHandler> DefaultHandler
$beforeOperationOneHandler->setNextHandler($beforeOperationTwoHandler);
$beforeOperationTwoHandler->setNextHandler($beforeOperationThreeHandler);
$beforeOperationThreeHandler->setNextHandler($defaultHandler);
return $beforeOperationOneHandler->Request($some_init);
}
它只是快速写成“责任链”模式的形状,所以我不假思索地复制了你的一些代码片段 我希望这会引导您找到更好的解决方案