/**
* 请假类
* Class LeaveRequest
*/
class LeaveRequest {
public $empName;
public $leaveDays;
public $reason;
public function __construct($empName, $leaveDays, $reason)
{
$this->empName = $empName;
$this->leaveDays = $leaveDays;
$this->reason = $reason;
}
}
/**
* 责任人
* Class Leader
*/
abstract class Leader {
protected $name;
/**
* @var Leader
*/
protected $nextLeader;
public function __construct($name)
{
$this->name = $name;
}
public function setNextLeader(Leader $nextLeader)
{
$this->nextLeader = $nextLeader;
}
public abstract function handleRequest(LeaveRequest $leaveRequest);
}
/**
* 主管
* Class Direct
*/
class Director extends Leader {
public function __construct($name)
{
parent::__construct($name);
}
/**
* 主管能批准三天以下的请假
* @param LeaveRequest $leaveRequest
*/
public function handleRequest(LeaveRequest $leaveRequest)
{
if ($leaveRequest->leaveDays <= 3) {
echo '请假人'.$leaveRequest->empName.',天数:'.$leaveRequest->leaveDays.',理由:'.$leaveRequest->reason.PHP_EOL;
echo '审批人:'.$this->name.PHP_EOL;
} else {
if (!empty($this->nextLeader)) {
$this->nextLeader->handleRequest($leaveRequest);
}
}
}
}
/**
* 经理
* Class Manager
*/
class Manager extends Leader {
public function __construct($name)
{
parent::__construct($name);
}
/**
* 经理批准三天以上十天以下的请假
* @param LeaveRequest $leaveRequest
*/
public function handleRequest(LeaveRequest $leaveRequest)
{
if ($leaveRequest->leaveDays <= 10) {
echo '请假人:'.$leaveRequest->empName.',天数:'.$leaveRequest->leaveDays.',理由:'.$leaveRequest->reason.PHP_EOL;
echo '审批人:'.$this->name.PHP_EOL;
} else {
if (!empty($this->nextLeader)) {
$this->nextLeader->handleRequest($leaveRequest);
}
}
}
}
/**
* 总经理
* Class GeneralManager
*/
class GeneralManager extends Leader {
public function __construct($name)
{
parent::__construct($name);
}
/**
* 总经理批准十天以上的请假
* @param LeaveRequest $leaveRequest
*/
public function handleRequest(LeaveRequest $leaveRequest)
{
echo '请假人'.$leaveRequest->empName.',天数:'.$leaveRequest->leaveDays.',理由:'.$leaveRequest->reason.PHP_EOL;
echo '审批人:'.$this->name.PHP_EOL;
}
}
class Client {
public function run()
{
// 构建责任链处理者
$director = new Director('主管');
$manager = new Manager('经理');
$generalManager = new GeneralManager('总经理');