目的:主要解決的是當控制一個對象狀態轉換的條件表達式過於複雜時的狀況。把狀態的判斷邏輯轉移到表示不一樣的一系列類當中,能夠把複雜的邏輯判斷簡單化。經過類來表示具體的case表達式,但這也致使了狀態邏輯分散到 不少子類中,難以維護ide
和策略模式的最大區別是 Contex吧State聲明爲了友元類,讓其能夠訪問contex 的能力。this
場景1: 用switch case case case 來決定多個狀態時,有限狀態自動機,這樣有幾個問題,1. 若是case太多。2.case 邏輯和實現沒有分離,很難 維護 和 擴展 spa
例1code
頭文件 class State; class Contex { public: Contex(State*state); void OperationInterface(); void Print(); private: void ChangeState(State*state); State*state = nullptr; friend class State; }; class State { public: virtual void PrintState() = 0; virtual void OperationInterface(Contex *contex) = 0; protected: void ChangeState(Contex*contex, State*state); }; class ConCreateStateA :public State { public: virtual void OperationInterface(Contex *contex)override; virtual void PrintState() override; }; class ConCreateStateB :public State { public: virtual void OperationInterface(Contex *contex)override; virtual void PrintState() override; }; void testState2(); 源文件 void Contex::Print() { state->PrintState(); } void Contex::OperationInterface() { state->OperationInterface(this); } void Contex::ChangeState(State*state) { this->state = state; } Contex::Contex(State*state) { this->state = state; } void State::ChangeState(Contex*contex, State*state) { contex->ChangeState(state); } void ConCreateStateA::PrintState() { cout << "current is A" << endl; } void ConCreateStateA::OperationInterface(Contex *contex) { this->ChangeState(contex, new ConCreateStateB); } void ConCreateStateB::OperationInterface(Contex *contex) { this->ChangeState(contex, new ConCreateStateA); } void ConCreateStateB::PrintState() { cout << "current is B" << endl; } void testState2() { Contex *contes = new Contex(new ConCreateStateA); contes->Print(); contes->OperationInterface(); contes->Print(); contes->OperationInterface(); contes->Print(); contes->OperationInterface(); }
例2對象
頭文件 #pragma once #include "PublicHeaders.h" namespace s1 { class Light; class SwitchState { public: virtual void PressSwitch(Light *light) = 0; virtual void PrintState() = 0; protected: void OnChangeState(Light*light, SwitchState* state); }; class On :public SwitchState { public: virtual void PressSwitch(Light *light); virtual void PrintState(); }; class Off :public SwitchState { virtual void PressSwitch(Light *light); virtual void PrintState(); }; class Light { public: Light(SwitchState *state); void PressSwitch(); void PrintCurrent(); private: SwitchState* state = nullptr; friend class SwitchState; }; void testState(); } 源文件 #include "State.h" namespace s1 { void SwitchState::OnChangeState(Light*light, SwitchState* state) { if (light->state) { delete light->state; } light->state = state; } void On::PressSwitch(Light *light) { this->OnChangeState(light, new Off); } void On::PrintState() { cout << "current is on" << endl; } void Off::PressSwitch(Light *light) { this->OnChangeState(light, new On); } void Off::PrintState() { cout << "current is off" << endl; } Light::Light(SwitchState *state) { this->state = state; } void Light::PressSwitch() { state->PressSwitch(this); } void Light::PrintCurrent() { state->PrintState(); } void testState() { Light *light = new Light(new Off); light->PrintCurrent(); light->PressSwitch(); light->PrintCurrent(); light->PressSwitch(); light->PrintCurrent(); light->PressSwitch(); light->PrintCurrent(); } }