设计模式-行为-State(状态)模式
目的:主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同的一系列类当中,可以把复杂的逻辑判断简单化。通过类来表示具体的case表达式,但这也导致了状态逻辑分散到 很多子类中,难以维护
和策略模式的最大区别是 Contex吧State声明为了友元类,让其可以访问contex 的能力。
场景1: 用switch case case case 来决定多个状态时,有限状态自动机,这样有几个问题,1. 如果case太多。2.case 逻辑和实现没有分离,很难 维护 和 扩展
例1
头文件 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(); } }