PHP狀態模式

來源:互聯網
上載者:User
####狀態設計模式* 狀態模式的作用是允許對象在狀態改變時改變其行為* 對象中頻繁的狀態非常依賴於條件陳述式,就自身來說條件陳述式並沒有什麼問題,不過,如果選項太多,以至於程式出現混亂,或者增加或改變選項需要太多的是時間。```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();```
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.