欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP状态模式

程序员文章站 2022-05-27 10:18:06
...
####状态设计模式* 状态模式的作用是允许对象在状态改变时改变其行为* 对象中频繁的状态非常依赖于条件语句,就自身来说条件语句并没有什么问题,不过,如果选项太多,以至于程序出现混乱,或者增加或改变选项需要太多的是时间。```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();```
相关标签: PHP状态模式