《大话设计模式》职责链模式-小菜请假加薪场景-PHP版代码

Standard

职责链模式
小菜请假加薪场景

<?php
/**
 * 职责链模式 小菜请假加薪场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
abstract class Manager{
        public $name;
        public $superior;
 
        public function __construct($name){
                $this->name = $name;
        }
        public function SetSuperior(Manager $superior){
                $this->superior = $superior;
        }
}
 
class Request{
        private $requestType;
        private $requestContent;
        private $number;
 
        public function __get($property_name){
                if(isset($this->$property_name)){
                        return($this->$property_name);
                }else{
                        return(NULL);
                }
        }
 
        public function __set($property_name, $value){
                $this->$property_name = $value;
        }
}
 
class CommonManager extends Manager{
        public function __construct($name){
                parent::__construct($name);
        }
        public function RequestApplications(Request $request){
                if( $request->RequestType == "请假" && $request->Number <=2 ){
                        echo $this->name .":". $request->RequestContent ."数量". $request->Number ."被批准\n";
                }else{
                        if( $this->superior != null ){
                                $this->superior->RequestApplications($request);
                        }
                }
        }
}
class MajorManager extends Manager{
        public function __construct($name){
                parent::__construct($name);
        }
        public function RequestApplications(Request $request){
                if( $request->RequestType == "请假" && $request->Number <=5 ){
                        echo $this->name .":". $request->RequestContent ."数量". $request->Number ."被批准\n";
                }else{
                        if( $this->superior != null ){
                                $this->superior->RequestApplications($request);
                        }
                }
        }
}
class GeneralManager extends Manager{
        public function __construct($name){
                parent::__construct($name);
        }
        public function RequestApplications(Request $request){
                if( $request->RequestType == "请假" ){
                        echo $this->name .":". $request->RequestContent ."数量". $request->Number ."被批准\n";
                }elseif( $request->RequestType == "加薪" && $request->Number <=500 ){
                        echo $this->name .":". $request->RequestContent ."数量". $request->Number ."被批准\n";
                }elseif( $request->RequestType == "加薪" && $request->Number > 500 ){
                        echo $this->name .":". $request->RequestContent ."数量". $request->Number ."被否决\n";
                }
        }
}
 
class Main {
    public static function run() {
                $jinli = new CommonManager("经理老张");
                $zhongjian = new MajorManager("总监老赵");
                $zhongjinli = new GeneralManager("总经理老刘");
 
                $jinli->SetSuperior($zhongjian);
                $zhongjian->SetSuperior($zhongjinli);
 
                $request1 = new Request();
                $request1->RequestType = "请假";
                $request1->RequestContent = "小菜请假";
                $request1->Number = 1;
                $jinli->RequestApplications($request1);
 
                $request2 = new Request();
                $request2->RequestType = "请假";
                $request2->RequestContent = "小菜请假";
                $request2->Number = 5;
                $jinli->RequestApplications($request2);
 
                $request3 = new Request();
                $request3->RequestType = "加薪";
                $request3->RequestContent = "小菜要求加薪";
                $request3->Number = 500;
                $jinli->RequestApplications($request3);
 
                $request4 = new Request();
                $request4->RequestType = "加薪";
                $request4->RequestContent = "小菜要求加薪";
                $request4->Number = 1000;
                $jinli->RequestApplications($request4);
    }
}
Main::run();
?>
//php ChainOfResponsibility.php 
//经理老张:小菜请假数量1被批准
//总监老赵:小菜请假数量5被批准
//总经理老刘:小菜要求加薪数量500被批准
//总经理老刘:小菜要求加薪数量1000被否决

转载请注明出处 http://yemaosheng.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.