• 设计模式-责任链模式


    <?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();
    
    
    ?>
    
  • 相关阅读:
    Mysql复制表格
    MySql的导入与导出
    jQuery语法
    JavaScript中的test()方法
    Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget(转)
    Android图片压缩方法总结
    Android实现app长时间未操作时自动退出app
    银行卡号的检测
    Android大图片裁剪终极解决方案(上:原理分析)
    Windows环境下Android Studio v1.0安装教程
  • 原文地址:https://www.cnblogs.com/ikasa007/p/4444012.html
Copyright © 2020-2023  润新知