时间:2021-07-01 10:21:17 帮助过:28人阅读
####状态设计模式* 状态模式的作用是允许对象在状态改变时改变其行为* 对象中频繁的状态非常依赖于条件语句,就自身来说条件语句并没有什么问题,不过,如果选项太多,以至于程序出现混乱,或者增加或改变选项需要太多的是时间。```offState = new OffState($this); $this->onState = new Onstate($this); $this->brighterState = new BrighterState($this); $this->brightestState = new BrightestState($this); $this->currentState = $this->offState; } public function turnOnLight(){ $this->currentState->turnLightOn(); } public function turnOffLight(){ $this->currentState->turnLightOff(); } public function turnBrighter(){ $this->currentState->turnBrighter(); } public function turnBrightest(){ $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; }}interface IState{ public function turnLightOn(); public function turnLightOff(); public function turnBrighter(); public function turnBrightest();}class Onstate implements IState{ private $context; public function __construct(Context $contextNew) { $this->context = $contextNew; } public function turnLightOff() { echo "Lights off".""; $this->context->setState($this->context->getOffState()); } public function turnLightOn() { echo "Light is already on there is no action".""; } public function turnBrighter() { echo "turn 明亮".""; } public function turnBrightest() { echo "turn 最亮".""; }}class OffState implements IState{ private $context; public function __construct(Context $contextNew) { $this->context = $contextNew; } public function turnLightOn() { echo "Lights On Now i can see".""; $this->context->setState($this->context->getOnState()); } public function turnLightOff() { echo "Light is already off".""; } public function turnBrightest() { echo "turn 最亮".""; } public function turnBrighter() { echo "turn 较亮".""; }}class BrighterState implements IState{ private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function turnBrighter() { echo "变暗了"; } public function turnBrightest() { $this->context->setState($this->context->getBrighterState()); } public function turnLightOff() { echo "关灯了"; } public function turnLightOn() { echo "灯是亮的"; }}class BrightestState implements IState{ private $context; public function __construct(Context $contextNow) { $this->context = $contextNow; } public function turnLightOff() { $this->context->setState($this->context->getOffState()); } public function turnBrightest() { echo "this is brightest".""; } public function turnBrighter() { echo "this is a little 暗淡".""; } public function turnLightOn() { echo "there is no use".""; }}class Client{ private $context; public function __construct() { $this->context = new Context(); $this->context->turnOnLight(); $this->context->turnOnLight(); $this->context->turnOffLight(); $this->context->turnOffLight(); $this->context->turnOnLight(); $this->context->turnBrighter(); $this->context->turnBrightest(); $this->context->turnOffLight(); }}$data = new Client();```