PHP設計模式——職責鏈模式

聲明:本系列博客參考資料《大話設計模式》,作者程傑。


       職責鏈模式(又叫責任鏈模式)包含了一些命令對象和一些處理對象,每個處理對象決定它能處理那些命令對象,它也知道應該把自己不能處理的命令對象交下一個處理對象,該模式還描述了往該鏈添加新的處理對象的方法。


       UML類圖:   

     


        角色:          

       抽象處理者(Manager)定義出一個處理請求的接口。如果需要,接口可以定義出一個方法,以設定和返回對下家的引用。這個角色通常由一個抽象類或接口實現。

       具體處理者(CommonManager)具體處理者接到請求後,可以選擇將請求處理掉,或者將請求傳給下家。由於具體處理者持有對下家的引用,因此,如果需要,具體處理者可以訪問下家。


      核心代碼:

<?php
/**
 * Created by PhpStorm.
 * User: Jang
 * Date: 2015/6/11
 * Time: 10:16
 */

//申請Model
class Request
{
    //數量
    public $num;
    //申請類型
    public $requestType;
    //申請內容
    public $requestContent;
}

//抽象管理者
abstract class Manager
{
    protected $name;
    //管理者上級
    protected $manager;
    public function __construct($_name)
    {
        $this->name = $_name;
    }

    //設置管理者上級
    public function SetHeader(Manager $_mana)
    {
        $this->manager = $_mana;
    }

    //申請請求
    abstract public function Apply(Request $_req);

}

//經理
class CommonManager extends Manager
{
    public function __construct($_name)
    {
        parent::__construct($_name);
    }
    public function Apply(Request $_req)
    {
        if($_req->requestType=="請假" && $_req->num<=2)
        {
            echo "{$this->name}:{$_req->requestContent} 數量{$_req->num}被批准。<br/>";
        }
        else
        {
            if(isset($this->manager))
            {
                $this->manager->Apply($_req);
            }
        }

    }
}

//總監
class MajorDomo extends Manager
{
    public function __construct($_name)
    {
        parent::__construct($_name);
    }

    public function Apply(Request $_req)
    {
        if ($_req->requestType == "請假" && $_req->num <= 5)
        {
            echo "{$this->name}:{$_req->requestContent} 數量{$_req->num}被批准。<br/>";
        }
        else
        {
            if (isset($this->manager))
            {
                $this->manager->Apply($_req);
            }
        }

    }
}


//總經理
class GeneralManager extends Manager
{
    public function __construct($_name)
    {
        parent::__construct($_name);
    }

    public function Apply(Request $_req)
    {
        if ($_req->requestType == "請假")
        {
            echo "{$this->name}:{$_req->requestContent} 數量{$_req->num}被批准。<br/>";
        }
        else if($_req->requestType=="加薪" && $_req->num <= 500)
        {
            echo "{$this->name}:{$_req->requestContent} 數量{$_req->num}被批准。<br/>";
        }
        else if($_req->requestType=="加薪" && $_req->num>500)
        {
            echo "{$this->name}:{$_req->requestContent} 數量{$_req->num}再說吧。<br/>";
        }
    }
}

            調用客戶端代碼:

           

header("Content-Type:text/html;charset=utf-8");
//--------------------職責鏈模式----------------------
require_once "./Responsibility/Responsibility.php";
$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、有多個對象可以處理同一個請求,具體哪個對象處理該請求由運行時刻自動確定。

          2、在不明確指定接收者的情況下,向多個對象中的一個提交一個請求。

         3、可動態指定一組對象處理請求。


           至此,PHP設計模式系列教程全部更新結束,歡迎大家批評指正。你的隻言片語是我前進的動力。


歡迎關注我的視頻課程,地址如下,謝謝。


   PHP面向對象設計模式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章