码迷,mamicode.com
首页 > Web开发 > 详细

职责链模式 - 设计模式 - PHP版

时间:2017-10-26 15:26:47      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:style   申请   detail   php   rdo   general   type   tail   nbsp   

  1 <?php
  2 /*
  3  * 职责链模式
  4  * 
  5  * 参考:http://blog.csdn.net/jhq0113/article/details/46454419
  6  * 
  7  */
  8 //申请Model  
  9 class Request {
 10     //数量  
 11     public $num;
 12     //申请类型  
 13     public $requestType;
 14     //申请内容  
 15     public $requestContent;
 16 }
 17 //抽象管理者  
 18 abstract class Manager {
 19     protected $name;
 20     //管理者上级  
 21     protected $manager;
 22     public function __construct($_name) {
 23         $this->name = $_name;
 24     }
 25     //设置管理者上级  
 26     public function SetHeader(Manager $_mana) {
 27         $this->manager = $_mana;
 28     }
 29     //申请请求  
 30     abstract public function Apply(Request $_req);
 31 }
 32 //经理  
 33 class CommonManager extends Manager {
 34     public function __construct($_name) {
 35         parent::__construct($_name);
 36     }
 37     public function Apply(Request $_req) {
 38         if ($_req->requestType == "请假" && $_req->num <= 2) {
 39             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";
 40         } else {
 41             if (isset($this->manager)) {
 42                 $this->manager->Apply($_req);
 43             }
 44         }
 45     }
 46 }
 47 //总监  
 48 class MajorDomo extends Manager {
 49     public function __construct($_name) {
 50         parent::__construct($_name);
 51     }
 52     public function Apply(Request $_req) {
 53         if ($_req->requestType == "请假" && $_req->num <= 5) {
 54             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";
 55         } else {
 56             if (isset($this->manager)) {
 57                 $this->manager->Apply($_req);
 58             }
 59         }
 60     }
 61 }
 62 //总经理  
 63 class GeneralManager extends Manager {
 64     public function __construct($_name) {
 65         parent::__construct($_name);
 66     }
 67     public function Apply(Request $_req) {
 68         if ($_req->requestType == "请假") {
 69             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";
 70         } else if ($_req->requestType == "加薪" && $_req->num <= 500) {
 71             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";
 72         } else if ($_req->requestType == "加薪" && $_req->num > 500) {
 73             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}再说吧。<br/>";
 74         }
 75     }
 76 }
 77 //--------------------客户端----------------------  
 78 $jingli = new CommonManager("李经理");
 79 $zongjian = new MajorDomo("郭总监");
 80 $zongjingli = new GeneralManager("孙总");
 81 //设置直接上级  
 82 $jingli->SetHeader($zongjian);
 83 $zongjian->SetHeader($zongjingli);
 84 //申请  
 85 $req1 = new Request();
 86 $req1->requestType = "请假";
 87 $req1->requestContent = "小菜请假!";
 88 $req1->num = 1;
 89 $jingli->Apply($req1);
 90 $req2 = new Request();
 91 $req2->requestType = "请假";
 92 $req2->requestContent = "小菜请假!";
 93 $req2->num = 4;
 94 $jingli->Apply($req2);
 95 $req3 = new Request();
 96 $req3->requestType = "加薪";
 97 $req3->requestContent = "小菜请求加薪!";
 98 $req3->num = 500;
 99 $jingli->Apply($req3);
100 $req4 = new Request();
101 $req4->requestType = "加薪";
102 $req4->requestContent = "小菜请求加薪!";
103 $req4->num = 1000;
104 $jingli->Apply($req4);

 

职责链模式 - 设计模式 - PHP版

标签:style   申请   detail   php   rdo   general   type   tail   nbsp   

原文地址:http://www.cnblogs.com/benben7466/p/7736547.html

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