1. 意图
允许一个对象在其内部状态改变时改变它的行为。
2. 三种角色
上下文环境(Context)、抽象状态(State)、具体状态(Concrete State)
3. 优点
3.1 将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。
3.2 使得状态转换显式化。
3.3 State对象可被共享。
4. 缺点
4.1 增加了子类的数目
5. 相关模式
5.1 状态对象通常用享元模式实现
5.2 状态对象通常是单件
6. 代码示意(C++)
#pragma once
#include <iostream>
using namespace std;class Context;class State
{
public:virtual void Handle(Context* pContext) = 0;
protected:State() {}void ChangeState(Context* pContext, State* pState);
};class ConcreteStateA :public State
{static ConcreteStateA* s_instance;
public:static State* Instance() {if (0 == s_instance) {s_instance = new ConcreteStateA;}return s_instance;}static void DelInstance() {delete s_instance;}
public:virtual void Handle(Context* pContext);
protected:ConcreteStateA() {}
};class ConcreteStateB :public State
{static ConcreteStateB* s_instance;
public:static State* Instance() {if (0 == s_instance) {s_instance = new ConcreteStateB;}return s_instance;}static void DelInstance() {delete s_instance;}
public:virtual void Handle(Context* pContext);
protected:ConcreteStateB() {}
};class Context
{State* m_pState;
public:Context() {m_pState = ConcreteStateA::Instance();}void Request() {if (0 != m_pState) {m_pState->Handle(this);}}
private:friend class State;void ChangeState(State* pState) {m_pState = pState;}
};
State.cpp:
#include "State.h"ConcreteStateA* ConcreteStateA::s_instance = 0;
ConcreteStateB* ConcreteStateB::s_instance = 0;void State::ChangeState(Context* pContext, State* pState) {pContext->ChangeState(pState);
}void ConcreteStateA::Handle(Context* pContext) {cout << "ConcreteStateA handled" << endl;ChangeState(pContext, ConcreteStateB::Instance());
}void ConcreteStateB::Handle(Context* pContext) {cout << "ConcreteStateB handled" << endl;
}
#include "State.h"
int main() {Context* pContext = new Context();pContext->Request();pContext->Request();delete pContext;return 0;
}
运行结果:
6.1 不同的ConcreteState,对应不同的处理方法(3.1)
6.2 状态对象比多个一般变量更明确,更容易理解和管理(3.2)
6.3 代码里的状态对象是单例共享的(3.3)
6.4 一个ConcreteState需要知道下一个状态是谁。