PHP状态模式

WBOY
发布: 2016-06-23 13:14:28
原创
1147 人浏览过

####状态设计模式* 状态模式的作用是允许对象在状态改变时改变其行为* 对象中频繁的状态非常依赖于条件语句,就自身来说条件语句并没有什么问题,不过,如果选项太多,以至于程序出现混乱,或者增加或改变选项需要太多的是时间。```<?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();```
登录后复制

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板