Heim  >  Artikel  >  Backend-Entwicklung  >  PHP状态模式

PHP状态模式

WBOY
WBOYOriginal
2016-06-23 13:14:281092Durchsuche

####状态设计模式* 状态模式的作用是允许对象在状态改变时改变其行为* 对象中频繁的状态非常依赖于条件语句,就自身来说条件语句并没有什么问题,不过,如果选项太多,以至于程序出现混乱,或者增加或改变选项需要太多的是时间。```<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/3/12 * Time: 下午3:57 */header("Content-type: text/html; charset=utf-8");class Context{    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);        $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"."</br>";        $this->context->setState($this->context->getOffState());    }    public function turnLightOn()    {        echo "Light is already on there is no action"."</br>";    }    public function turnBrighter()    {        echo "turn 明亮"."</br>";    }    public function turnBrightest()    {        echo "turn 最亮"."</br>";    }}class OffState implements IState{    private $context;    public function __construct(Context $contextNew)    {     $this->context = $contextNew;    }    public function turnLightOn()    {       echo "Lights On Now i can see"."</br>";        $this->context->setState($this->context->getOnState());    }    public function turnLightOff()    {       echo "Light is already off"."</br>";    }    public function turnBrightest()    {        echo "turn 最亮"."</br>";    }    public function turnBrighter()    {        echo "turn 较亮"."</br>";    }}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"."</br>";    }    public function turnBrighter()    {       echo "this is a little 暗淡"."</br>";    }    public function turnLightOn()    {        echo "there is no use"."</br>";    }}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();```

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn