-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.php
88 lines (72 loc) · 1.86 KB
/
test.php
1
<?php/** * Created by PhpStorm. * User: chentao * Date: 2021/7/3 * Time: 8:59 PM */class Bug { public $value; public function __construct(int $value) { $this->value = $value; }}abstract class Programmer { protected $next; public function setNext(Programmer $next) { $this->next = $next; } abstract function handle(Bug $bug);}class NewbieProgrammer extends Programmer{ public function handle(Bug $bug){ if ($bug->value > 0 && $bug->value <= 20) { $this->solve($bug); } elseif ($this->next != null){ $this->next->handle($bug); } } private function solve(Bug $bug){ echo "菜鸟程序员解决一个难度为:".$bug->value."的bug".PHP_EOL; }}/** * 中级程序员 * Class MiddleProgrammer */class MiddleProgrammer extends Programmer{ public function handle(Bug $bug){ if ($bug->value > 20 && $bug->value <= 50) { $this->solve($bug); } elseif ($this->next != null){ $this->next->handle($bug); } } public function solve(Bug $bug){ echo "中级程序员解决一个难度为:".$bug->value."的bug".PHP_EOL; }}class AdvanceProgrammer extends Programmer{ public function handle(Bug $bug){ if ($bug->value > 50) { $this->solve($bug); } elseif ($this->next != null){ $this->next->handle($bug); } } public function solve(Bug $bug){ echo "高级程序员解决一个难度为:".$bug->value."的bug".PHP_EOL; }}$easy = new Bug(10);$normal = new Bug(25);$hard = new Bug(60);$newbie = new NewbieProgrammer();$middle= new MiddleProgrammer();$advance = new AdvanceProgrammer();$newbie->setNext($middle);$middle->setNext($advance);$newbie->handle($easy);$newbie->handle($normal);$newbie->handle($hard);