职责链模式

职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/2/28

 * Time: 11:37

 */

 

namespace frontend\models;

 

 

class Request

{

 

    //数量

    public  $num;

 

    //申请类型

    public  $requestType;

 

    //申请内容

    public $requestContent;

 

 

}

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/2/28

 * Time: 11:39

 */

 

namespace frontend\models;

 

//抽象管理者

abstract class Manager

{

 

    protected  $name;

 

    //管理者上级

    protected  $manager;

 

    public  function __construct($name)

    {

        $this->name = $name;

    }

 

    //设置管理者上级

    public  function setHeader($manager){

 

        $this->manager = $manager;

    }

 

    abstract  public  function Apply(Request $request);

 

 

 

}

 

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/2/28

 * Time: 11:46

 */

 

namespace frontend\models;

 

 

class CommonManager extends Manager

{

 

    public function __construct($name)

    {

        parent::__construct($name);

    }

 

 

    public function Apply(Request $request)

    {

 

        if ($request->requestType == '请假' && $request->num < 2){

 

            echo "{$this->name}:{$request->requestContent} 数量{$request->num}被批准。<br/>";

        }else{

 

            if(isset($this->manager)){

 

                $this->manager->Apply($request);

            }

        }

    }

 

}

 

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/2/28

 * Time: 11:51

 */

 

namespace frontend\models;

 

 

class MajorDomo extends Manager

{

    public function __construct($name)

    {

        parent::__construct($name);

    }

 

    public function Apply(Request $request)

    {

        if ($request->requestType == '请假' && $request->num <= 5){

 

            echo "{$this->name}:{$request->requestContent} 数量{$request->num}被批准。<br/>";

        }else{

 

            if(isset($this->manager)){

 

                $this->manager->Apply($request);

            }

        }

    }

 

}

<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/2/28

 * Time: 11:52

 */

 

namespace frontend\models;

 

 

class GeneralManager extends Manager

{

    public function __construct($name)

    {

        parent::__construct($name);

    }

 

    public function Apply(Request $request)

    {

        if ($request->requestType == "请假")

        {

            echo "{$this->name}:{$request->requestContent} 数量{$request->num}被批准。<br/>";

        }

        else if($request->requestType=="加薪" && $request->num <= 500)

        {

            echo "{$this->name}:{$request->requestContent} 数量{$request->num}被批准。<br/>";

        }

        else if($request->requestType=="加薪" && $request->num>500)

        {

            echo "{$this->name}:{$request->requestContent} 数量{$request->num}再说吧。<br/>";

        }

    }

 

}

 

 $jingli = new CommonManager("李经理");

        $zongjian = new MajorDomo("郭总监");

        $zongjingli = new GeneralManager("孙总");

 

        //设置直接上级

        $jingli->SetHeader($zongjian);

        $zongjian->SetHeader($zongjingli);

 

//申请

        $req1 = new Request();

        $req1->requestType = "请假";

        $req1->requestContent = "小菜请假!";

        $req1->num = 1;

        $jingli->Apply($req1);

 

        $req2 = new Request();

        $req2->requestType = "请假";

        $req2->requestContent = "小菜请假!";

        $req2->num = 4;

        $jingli->Apply($req2);

 

        $req3 = new Request();

        $req3->requestType = "加薪";

        $req3->requestContent = "小菜请求加薪!";

        $req3->num = 500;

        $jingli->Apply($req3);

 

        $req4 = new Request();

        $req4->requestType = "加薪";

        $req4->requestContent = "小菜请求加薪!";

        $req4->num = 1000;

        $jingli->Apply($req4);

 

  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Jang 
  5.  * Date: 2015/6/11 
  6.  * Time: 10:16 
  7.  */  
  8.   
  9. //申请Model  
  10. class Request  
  11. {  
  12.     //数量  
  13.     public $num;  
  14.     //申请类型  
  15.     public $requestType;  
  16.     //申请内容  
  17.     public $requestContent;  
  18. }  
  19.   
  20. //抽象管理者  
  21. abstract class Manager  
  22. {  
  23.     protected $name;  
  24.     //管理者上级  
  25.     protected $manager;  
  26.     public function __construct($_name)  
  27.     {  
  28.         $this->name = $_name;  
  29.     }  
  30.   
  31.     //设置管理者上级  
  32.     public function SetHeader(Manager $_mana)  
  33.     {  
  34.         $this->manager = $_mana;  
  35.     }  
  36.   
  37.     //申请请求  
  38.     abstract public function Apply(Request $_req);  
  39.   
  40. }  
  41.   
  42. //经理  
  43. class CommonManager extends Manager  
  44. {  
  45.     public function __construct($_name)  
  46.     {  
  47.         parent::__construct($_name);  
  48.     }  
  49.     public function Apply(Request $_req)  
  50.     {  
  51.         if($_req->requestType=="请假" && $_req->num<=2)  
  52.         {  
  53.             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";  
  54.         }  
  55.         else  
  56.         {  
  57.             if(isset($this->manager))  
  58.             {  
  59.                 $this->manager->Apply($_req);  
  60.             }  
  61.         }  
  62.   
  63.     }  
  64. }  
  65.   
  66. //总监  
  67. class MajorDomo extends Manager  
  68. {  
  69.     public function __construct($_name)  
  70.     {  
  71.         parent::__construct($_name);  
  72.     }  
  73.   
  74.     public function Apply(Request $_req)  
  75.     {  
  76.         if ($_req->requestType == "请假" && $_req->num <= 5)  
  77.         {  
  78.             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";  
  79.         }  
  80.         else  
  81.         {  
  82.             if (isset($this->manager))  
  83.             {  
  84.                 $this->manager->Apply($_req);  
  85.             }  
  86.         }  
  87.   
  88.     }  
  89. }  
  90.   
  91.   
  92. //总经理  
  93. class GeneralManager extends Manager  
  94. {  
  95.     public function __construct($_name)  
  96.     {  
  97.         parent::__construct($_name);  
  98.     }  
  99.   
  100.     public function Apply(Request $_req)  
  101.     {  
  102.         if ($_req->requestType == "请假")  
  103.         {  
  104.             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";  
  105.         }  
  106.         else if($_req->requestType=="加薪" && $_req->num <= 500)  
  107.         {  
  108.             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。<br/>";  
  109.         }  
  110.         else if($_req->requestType=="加薪" && $_req->num>500)  
  111.         {  
  112.             echo "{$this->name}:{$_req->requestContent} 数量{$_req->num}再说吧。<br/>";  
  113.         }  
  114.     }  
  115. }  
posted on 2018-02-28 11:59  paulversion  阅读(117)  评论(0编辑  收藏  举报