设计理念
允许对象在内部状态改变时改变他的行为。
状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同的一系列类当中,可以把复杂的逻辑判断简单化。状态模式通过把各种状态转移逻辑分不到State的子类之间,相比于通过硬编码的状态机会减少相互间的依赖。
UML类图
- Context: 环境类,它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关的操作委托给当前的Concrete State对象来处理。
- State: 抽象状态类,定义一个接口以封装使用上下文环境的的一个特定状态相关的行为。
- ConcreteState: 具体状态类,实现抽象状态定义的接口。
代码实现
#include <iostream>
#include "Context.h"
#include "ConcreteStateA.h"
#include "ConcreteStateB.h"
using namespace std;
int main(int argc, char *argv[])
{
char a = '0';
if('0' == a)
cout << "yes" << endl;
else
cout << "no" << endl;
Context * c = new Context();
c->request();
c->request();
c->request();
delete c;
return 0;
}
///////////////////////////////////////////////////////////
// Context.h
// Implementation of the Class Context
///////////////////////////////////////////////////////////
#include "State.h"
class Context
{
public:
Context();
virtual ~Context();
void changeState(State * st);
void request();
private:
State *m_pState;
};
///////////////////////////////////////////////////////////
// Context.cpp
// Implementation of the Class Context
///////////////////////////////////////////////////////////
#include "Context.h"
#include "ConcreteStateA.h"
Context::Context(){
//default is a
m_pState = ConcreteStateA::Instance();
}
Context::~Context(){
}
void Context::changeState(State * st){
m_pState = st;
}
void Context::request(){
m_pState->handle(this);
}
///////////////////////////////////////////////////////////
// ConcreteStateA.h
// Implementation of the Class ConcreteStateA
///////////////////////////////////////////////////////////
#include "State.h"
class ConcreteStateA : public State
{
public:
virtual ~ConcreteStateA();
static State * Instance();
virtual void handle(Context * c);
private:
ConcreteStateA();
static State * m_pState;
};
///////////////////////////////////////////////////////////
// ConcreteStateA.cpp
// Implementation of the Class ConcreteStateA
///////////////////////////////////////////////////////////
#include "ConcreteStateA.h"
#include "ConcreteStateB.h"
#include "Context.h"
#include <iostream>
using namespace std;
State * ConcreteStateA::m_pState = NULL;
ConcreteStateA::ConcreteStateA(){
}
ConcreteStateA::~ConcreteStateA(){
}
State * ConcreteStateA::Instance()
{
if ( NULL == m_pState)
{
m_pState = new ConcreteStateA();
}
return m_pState;
}
void ConcreteStateA::handle(Context * c){
cout << "doing something in State A.\n done,change state to B" << endl;
c->changeState(ConcreteStateB::Instance());
}
总结
这个示例来自《设计模式》,展示了一个简化版的TCP协议实现; TCP连接的状态有多种可能,状态之间的转换有相应的逻辑前提; 这是使用状态模式的场合;
状态模式描述了对象状态的变化以及对象如何在每一种状态下表现出不同的行为。
状态模式的主要优点在于封装了转换规则,并枚举可能的状态,它将所有与某个状态有关的行为放到一个类中,并且可以方便地增加新的状态,只需要改变对象状态即可改变对象的行为,还可以让多个环境对象共享一个状态对象,从而减少系统中对象的个数;其缺点在于使用状态模式会增加系统类和对象的个数,且状态模式的结构与实现都较为复杂,如果使用不当将导致程序结构和代码的混乱,对于可以切换状态的状态模式不满足“开闭原则”的要求。