当前位置:Gxlcms > PHP教程 > PHP设计模式——职责链模式_PHP教程

PHP设计模式——职责链模式_PHP教程

时间:2021-07-01 10:21:17 帮助过:3人阅读

PHP设计模式——职责链模式


职责链模式(又叫责任链模式)包含了一些命令对象和一些处理对象,每个处理对象决定它能处理那些命令对象,它也知道应该把自己不能处理的命令对象交下一个处理对象,该模式还描述了往该链添加新的处理对象的方法。

UML类图:

\

角色:

抽象处理者(Manager):定义出一个处理请求的接口。如果需要,接口可以定义出一个方法,以设定和返回对下家的引用。这个角色通常由一个抽象类或接口实现。

具体处理者(CommonManager):具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。


核心代码:

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}被批准。
;
        }
        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}被批准。
;
        }
        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}被批准。
;
        }
        else if($_req->requestType==加薪 && $_req->num <= 500)
        {
            echo {$this->name}:{$_req->requestContent} 数量{$_req->num}被批准。
;
        }
        else if($_req->requestType==加薪 && $_req->num>500)
        {
            echo {$this->name}:{$_req->requestContent} 数量{$_req->num}再说吧。
;
        }
    }
}

调用客户端代码:

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设计模式系列教程全部更新结束,欢迎大家批评指正。你的只言片语是我前进的动力。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1015539.htmlTechArticlePHP设计模式——职责链模式 职责链模式(又叫责任链模式)包含了一些命令对象和一些处理对象,每个处理对象决定它能处理那些命令对象...

人气教程排行