職責(zé)鏈模式
職責(zé)鏈模式(又叫責(zé)任鏈模式)包含了一些命令對(duì)象和一些處理對(duì)象随夸,每個(gè)處理對(duì)象決定它能處理那些命令對(duì)象驻民,它也知道應(yīng)該把自己不能處理的命令對(duì)象交下一個(gè)處理對(duì)象附鸽,該模式還描述了往該鏈添加新的處理對(duì)象的方法弦撩。
Demo
<?php
/**
* Created by PhpStorm.
*/
//申請(qǐng)Model
class Request
{
//數(shù)量
public $num;
//申請(qǐng)類型
public $requestType;
//申請(qǐng)內(nèi)容
public $requestContent;
}
//抽象管理者
abstract class Manager
{
protected $name;
//管理者上級(jí)
protected $manager;
public function __construct($_name)
{
$this->name = $_name;
}
//設(shè)置管理者上級(jí)
public function SetHeader(Manager $_mana)
{
$this->manager = $_mana;
}
//申請(qǐng)請(qǐng)求
abstract public function Apply(Request $_req);
}
//經(jīng)理
class CommonManager extends Manager
{
public function __construct($_name)
{
parent::__construct($_name);
}
public function Apply(Request $_req)
{
if($_req->requestType=="請(qǐng)假"&& $_req->num<=2)
{
echo "{$this->name}:{$_req->requestContent} 數(shù)量{$_req->num}被批準(zhǔn)昨稼。<br/>";
}
else
{
if(isset($this->manager))
{
$this->manager->Apply($_req);
}
}
}
}
//總監(jiān)
class MajorDomo extends Manager
{
public function __construct($_name)
{
parent::__construct($_name);
}
public function Apply(Request $_req)
{
if ($_req->requestType == "請(qǐng)假"&& $_req->num <= 5)
{
echo "{$this->name}:{$_req->requestContent} 數(shù)量{$_req->num}被批準(zhǔn)节视。<br/>";
}
else
{
if (isset($this->manager))
{
$this->manager->Apply($_req);
}
}
}
}
//總經(jīng)理
class GeneralManager extends Manager
{
public function __construct($_name)
{
parent::__construct($_name);
}
public function Apply(Request $_req)
{
if ($_req->requestType == "請(qǐng)假")
{
echo "{$this->name}:{$_req->requestContent} 數(shù)量{$_req->num}被批準(zhǔn)。<br/>";
}
else if($_req->requestType=="加薪"&& $_req->num <= 500)
{
echo "{$this->name}:{$_req->requestContent} 數(shù)量{$_req->num}被批準(zhǔn)假栓。<br/>";
}
else if($_req->requestType=="加薪"&& $_req->num>500)
{
echo "{$this->name}:{$_req->requestContent} 數(shù)量{$_req->num}再說(shuō)吧寻行。<br/>";
}
}
}
$jingli = new CommonManager("李經(jīng)理");
$zongjian = new MajorDomo("郭總監(jiān)");
$zongjingli = new GeneralManager("孫總");
//設(shè)置直接上級(jí)
$jingli->SetHeader($zongjian);
$zongjian->SetHeader($zongjingli);
//申請(qǐng)
$req1 = new Request();
$req1->requestType = "請(qǐng)假";
$req1->requestContent = "小菜請(qǐng)假!";
$req1->num = 1;
$jingli->Apply($req1);
$req2 = new Request();
$req2->requestType = "請(qǐng)假";
$req2->requestContent = "小菜請(qǐng)假匾荆!";
$req2->num = 4;
$jingli->Apply($req2);
$req3 = new Request();
$req3->requestType = "加薪";
$req3->requestContent = "小菜請(qǐng)求加薪拌蜘!";
$req3->num = 500;
$jingli->Apply($req3);
$req4 = new Request();
$req4->requestType = "加薪";
$req4->requestContent = "小菜請(qǐng)求加薪!";
$req4->num = 1000;
$jingli->Apply($req4);