public function check(HomeStatus $home) { if (!$home->lightsOff) { printf("The lights are still on!! Abort abort.\n"); } $this->next($home); } } /** * Class Alarm */ class Alarm extends HomeChecker { public function check(HomeStatus $home) { if (!$home->alarmOn) { printf("The alarm has not been set!! Abort abort.\n"); } $this->next($home); } } // create new instances $locks = new Locks(); $lights = new Lights(); $alarm = new Alarm(); // set the successor chain $locks->succeedWith($lights); $lights->succeedWith($alarm); // starts off here... $locks->check(new HomeStatus());
{ if (!$home->alarmOn) { throw new Exception('The alarm is off!! Abort'); } $this->next($home); } } class Lights extends HomeChecker { public function check(HomeStatus $home) { if (!$home->lightsOff) { throw new Exception('The lights is on!! Abort.'); } $this->next($home); } } class HomeStatus { public $locked = true; public $alarmOn = true; public $lightsOff = true; } $status = new HomeStatus(); $locks = new Locks(); $alarm = new Alarm(); $lights = new Lights(); $locks->chainedWith($lights); $lights->chainedWith($alarm); $locks->check($status); return 0;
<?php /** * Created by PhpStorm. * User: B * Date: 12/25/2015 * Time: 12:33 AM */ include_once 'HomeData.php'; include_once 'Locks.php'; include_once 'Lights.php'; include_once 'Alarm.php'; $myLocks = new Locks(); $myLights = new Lights(); $myAlarm = new Alarm(); $myLocks->succeedWith($myLights); $myLights->succeedWith($myAlarm); $myLocks->check(new HomeData());