码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式-责任链模式

时间:2015-04-21 12:57:25      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

<?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();


?>

 

设计模式-责任链模式

标签:

原文地址:http://www.cnblogs.com/ikasa007/p/4444012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!