<?php abstract class Handler{ protected $_handler = null; protected $_handlerName = null; public function setSuccessor($handler){ $this->_handler = $handler; } protected function _success($request){ echo $request->getName(),'request was passed <br/>'; return true; } abstract function handleRequest($request); } class ConcreteHandlerLeader extends Handler{ public function __construct($handlerName){ $this->_handlerName = $handlerName; } public function handleRequest($request){ echo $this->_handlerName." has know the rest <br/>"; if(($request->getDay()) < 0.5){ $this->_success($request); } if($this->_handler instanceof Handler){ $this->_handler->handleRequest($request); } } } class ConcreteHandlerManager extends Handler{ public function __construct($handlerName){ $this->_handlerName = $handlerName; } public function handleRequest($request){ echo $this->_handlerName." has know the rest <br/>"; if($request->getDay() <= 3 && $request->getDay() > 0.5){ } if($this->_handler instanceof Handler){ $this->_handler->handleRequest($request); } } } class ConcreteHandlerBoss extends Handler{ public function __construct($handlerName){ $this->_handlerName = $handlerName; } public function handleRequest($request){ echo $this->_handlerName." has know the rest <br/>"; if(($request->getDay()) > 3){ $this->_success($request); } if($this->_handler instanceof Handler){ $this->_handler->handleRequest($request); } } } class Request{ private $_name; private $_day; private $_reason; function __construct($name= '', $day= 0, $reason = ''){ $this->_name = $name; $this->_day = $day; $this->_reason = $reason; } public function setName($name){ $this->_name = $name; } public function getName(){ return $this->_name; } public function setDay($day){ $this->_day = $day; } public function getDay(){ return $this->_day ; } public function setReason($reason ){ $this->_reason = $reason; } public function getReason( ){ return $this->_reason; } } class client{ /** *流程1:leader-> manager ->generalManager * */ static function main(){ $leader = new ConcreteHandlerLeader('leader'); $manager = new ConcreteHandlerManager('manager'); $generalManager = new ConcreteHandlerBoss('generalManager'); //请求实例 $request = new Request('guisu',4,'休息' ); $leader->setSuccessor($manager); $manager->setSuccessor($generalManager); $result = $leader->handleRequest($request); } /** * 流程2 : * leader ->generalManager */ static function main2(){ //签字列表 $leader = new ConcreteHandlerLeader('leader'); $manager = new ConcreteHandlerManager('manager'); $generalManager = new ConcreteHandlerBoss('generalManager'); //请求实例 $request = new Request('guisu',3,'休息' ); $leader->setSuccessor($generalManager); $result = $leader->handleRequest($request); } /** * 流程3 :如果leader不在,那么完全可以写这样的代码 * manager ->generalManager */ static function main3(){ //签字列表 $leader = new ConcreteHandlerLeader('leader'); $manager = new ConcreteHandlerManager('manager'); $generalManager = new ConcreteHandlerBoss('generalManager'); //请求实例 $request = new Request('guisu',0.1,'休息' ); $leader->setSuccessor($request); $manager->setSuccessor($generalManager); $result = $manager->handleRequest($request); } } client::main(); ?>