/**
* Created by PhpStorm.
* User: insei
* Date: 2019/1/27
* Time: 7:44 PM
*/
// 职责链模式(又叫做责任链模式) 包含了一些命令对象和一些处理对象,每个处理对象决定它能
// 出来那些对象,它也知道把自己不能处理的命令交给下一个处理对象,该模式还描述了往该链添加
// 新的处理对象方法。
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}被批准。
";
} else {
if (isset($this->manager)) {
$this->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}再说吧。
";
}
}
}
//--------------------职责链模式----------------------
$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);