php责任链模式的订单,PHP 实现责任链模式

试用场景 多个对象可以处理同一个请求,具体是哪一个对象处理该请求由运行时刻自动决定;

角色分析:

1、抽象处理者(handler)角色

2、具体处理者(handler)角色

3、客户类角色

a6bf7c6cc6faf8b53e11c5c3c4fce44d.png

abstract class Handler

{

protected $_nextHandler;

public function getNextHandler()

{

return $this->_nextHandler;

}

public function setNextHandler(Handler $nextHandler)

{

$this->_nextHandler = $nextHandler;

}

abstract function handleMessage(int $type);

}

class ConcreteHandler1 extends Handler

{

public function handleMessage(int $type)

{

if($type % 2 == 0) {

echo "ConcreteHandler1 solved the problems".PHP_EOL;

} else {

echo "ConcreteHandler1 can not solved the problems".PHP_EOL;

if($this->_nextHandler != null) {

$this->_nextHandler->handleMessage($type);

} else {

echo "no one can solve the problem!".PHP_EOL;

}

}

}

}

class ConcreteHandler2 extends Handler

{

public function handleMessage(int $type)

{

if($type % 5 == 0) {

echo "ConcreteHandler2 solved the problems".PHP_EOL;

} else {

echo "ConcreteHandler2 can not solved the problems".PHP_EOL;

if($this->_nextHandler != null) {

$this->_nextHandler->handleMessage($type);

} else {

echo "no one can solve the problem!".PHP_EOL;

}

}

}

}

class ConcreteHandler3 extends Handler

{

public function handleMessage(int $type)

{

if($type % 3 == 0) {

echo "ConcreteHandler3 solved the problems".PHP_EOL;

} else {

echo "ConcreteHandler3 can not solved the problems".PHP_EOL;

if($this->_nextHandler != null) {

$this->_nextHandler->handleMessage($type);

} else {

echo "no one can solve the problem!".PHP_EOL;

}

}

}

}

$handler1 = new ConcreteHandler1();

$handler2 = new ConcreteHandler2();

$handler3 = new ConcreteHandler3();

$handler1->setNextHandler($handler2);

$handler2->setNextHandler($handler3);

for ($i = 0; $i < 10; $i++) {

echo "proceeding message...".$i.PHP_EOL;

$handler1->handleMessage($i);

}

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值