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面向对象设计模式

目录
相关文章
|
7天前
|
设计模式 算法 PHP
php设计模式--策略模式(六)
php设计模式--策略模式(六)
10 0
|
7天前
|
设计模式 PHP
php设计模式--装饰模式(七)装饰模式完成文章编辑
php设计模式--装饰模式(七)装饰模式完成文章编辑
9 0
|
4月前
|
设计模式
二十三种设计模式:解密职责链模式-购物优惠活动的设计艺术
二十三种设计模式:解密职责链模式-购物优惠活动的设计艺术
|
4月前
|
设计模式
二十三种设计模式全面解析-职责链模式的高级应用-日志记录系统
二十三种设计模式全面解析-职责链模式的高级应用-日志记录系统
|
1天前
|
设计模式 Java
小谈设计模式(25)—职责链模式
小谈设计模式(25)—职责链模式
|
7天前
|
设计模式 PHP
php设计模式--责任链模式(五)
php设计模式--责任链模式(五)
8 0
|
6月前
|
设计模式 缓存 Java
行为型设计模式08-职责链模式
行为型设计模式08-职责链模式
19 0
|
4月前
|
设计模式
二十三种设计模式全面解析-解密职责链模式:请求处理的设计艺术
二十三种设计模式全面解析-解密职责链模式:请求处理的设计艺术
|
4月前
|
设计模式
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
|
4月前
|
设计模式 Java 应用服务中间件
认真学习设计模式之职责链模式((Chain of Responsibility Pattern)
认真学习设计模式之职责链模式((Chain of Responsibility Pattern)
63 0