PHP设计模式之状态模式定义与用法详解(2)

对于所有的设计模式来说,很重要的一个方面是: 利用这些设计模式可以很容易地做出修改. 与其他模式一样,状态模式也很易于更新和改变. 下面在这个灯的示例上再加两个状态:更亮(Brighter)和最亮(Brightest)

现在变成了4个状态, 序列有所改变. '关'(off)状态只能变到"开"(on)状态, on状态不能变到off状态. on状态只能变到"更亮"(brighter)状态和"最亮"(brightest)状态. 只能最亮状态才可能变到关状态.

改变接口

要改变的第一个参与者是接口IState, 这个接口中必须指定相应的方法, 可以用来迁移到brighter和brightest状态.

IState.php

<?php interface IState { public function turnLightOn(); public function turnLightOff(); public function turnBrighter(); public function turnBrightest(); }

现在所有状态类都必须包含这4个方法, 它们都需要结合到Light类中.

改变状态

状态设计模式中有改变时, 这些新增的改变会对模式整体的其他方面带来影响. 不过, 增加改变相当简单, 每个状态只有一个特定的变迁.

四个状态

OnState.php

<?php class OnState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "不合法的操作!<br />"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "灯更亮了, 看帅哥chenqionghe看得更真切了!<br />"; $this->light->setState($this->light->getBrighterState()); } public function turnBrightest() { echo "不合法的操作!<br />"; } }

OffState.php

<?php class OffState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯打开!可以看见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOnState()); } public function turnLightOff() { echo "不合法的操作!<br />"; } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "不合法的操作!<br />"; } }

Brighter.php

<?php class BrighterState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "不合法的操作!<br />"; } public function turnLightOff() { echo "不合法的操作!<br />"; } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "灯最亮了, 看帅哥chenqionghe已经帅到无敌!<br />"; $this->light->setState($this->light->getBrightestState()); } }

Brightest.php

<?php class BrightestState implements IState { private $light; public function __construct(Light $light) { $this->light = $light; } public function turnLightOn() { echo "灯已经打开了->不做操作<br />"; } public function turnLightOff() { echo "灯关闭!看不见帅哥chenqionghe了!<br />"; $this->light->setState($this->light->getOffState()); } public function turnBrighter() { echo "不合法的操作!<br />"; } public function turnBrightest() { echo "不合法的操作!<br />"; } }

更新Light类

Light.php

<?php class Light { private $offState; //关闭状态 private $onState; //开启状态 private $brighterState; //更亮状态 private $brightestState;//最亮状态 private $currentState; //当前状态 public function __construct() { $this->offState = new OffState($this); $this->onState = new OnState($this); $this->brighterState = new BrighterState($this); $this->brightestState = new BrightestState($this); //开始状态为关闭状态Off $this->currentState = $this->offState; } //调用状态方法触发器 public function turnLightOn() { $this->currentState->turnLightOn(); } public function turnLightOff() { $this->currentState->turnLightOff(); } public function turnLightBrighter() { $this->currentState->turnBrighter(); } public function turnLigthBrightest() { $this->currentState->turnBrightest(); } //设置当前状态 public function setState(IState $state) { $this->currentState = $state; } //获取状态 public function getOnState() { return $this->onState; } public function getOffState() { return $this->offState; } public function getBrighterState() { return $this->brighterState; } public function getBrightestState() { return $this->brightestState; } }

更新客户

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4144944a304b9040a6a4f6afee5dcb92.html