状态模式State
在组件构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的稳定?“状态变化”模式为这一问题提供了一种解决方案。
经典模式:State、Memento
动机(Motivation)
在软件构建过程中,某些对象的状态如何改变,其行为也随之而发生变化,比如文档处于只读状态,其支持的行为和读写状态支持的行为就可以完全不同。
如何在运行时根据对象的状态来透明地更改对象的行为?而不会为对象操作和状态转化之间引入耦合?
示例1
enum NetworkState
{
Network_Open,
Network_Close,
Network_Connect,
NetWork_Wait // 新增状态
};
class NetworkProcessor {
NetworkState state_;
public:
void Operation1() {
if (Network_Open == state_) {
//***
state_ = Network_Close;
}
else if(Network_Close == state_)
{
//...
state_ = Network_Connect;
}
else if (Network_Connect == state_) {
//$$$
state_ = Network_Open;
}
// 变化
else if (NetWork_Wait == state_) {
//---
}
}
void Operation2() {
if (Network_Open == state_) {
//***
state_ = Network_Connect;
}
else if (Network_Close == state_)
{
//...
state_ = Network_Open;
}
else if (Network_Connect == state_) {
//$$$
state_ = Network_Close;
}
// 变化
else if (NetWork_Wait == state_) {
//---
}
}
void Operation3() {
//...
}
};
示例2
class NetworkState {
public:
NetworkState* pNext;
virtual void Operation1() = 0;
virtual void Operation2() = 0;
virtual void Operation3() = 0;
virtual ~NetworkState() {}
};
class OpenState : public NetworkState {
static NetworkState* m_instance;
public:
static NetworkState* getInstance() {
if (nullptr == m_instance) {
m_instance = new OpenState();
}
return m_instance;
}
virtual void Operation1() {
//***
pNext = CloseState::getInstance();
}
virtual void Operation2() {
//...
pNext = ConnectState::getInstance();
}
virtual void Operation3() {
//$$$
pNext = OpenState::getInstance();
}
};
class CloseState : public NetworkState {
static NetworkState* m_instance;
public:
static NetworkState* getInstance() {
if (nullptr == m_instance) {
m_instance = new CloseState();
}
return m_instance;
}
virtual void Operation1() {
//***
pNext = ConnectState::getInstance();
}
virtual void Operation2() {
//...
pNext = OpenState::getInstance();
}
virtual void Operation3() {
//$$$
pNext = CloseState::getInstance();
}
};
class ConnectState : public NetworkState {
static NetworkState* m_instance;
public:
static NetworkState* getInstance() {
if (nullptr == m_instance) {
m_instance = new ConnectState();
}
return m_instance;
}
virtual void Operation1() {
//***
pNext = OpenState::getInstance();
}
virtual void Operation2() {
//...
pNext = CloseState::getInstance();
}
virtual void Operation3() {
//$$$
pNext = ConnectState::getInstance();
}
};
// 扩展
class WaitState : public NetworkState {
//...
};
class NetworkProcessor {
NetworkState* state_;
public:
NetworkProcessor(NetworkState* state) {
this->state_ = state;
}
void Operation1() {
//...
state_->Operation1();
state_ = state_->pNext;
//...
}
void Operation2() {
//...
state_->Operation2();
state_ = state_->pNext;
//...
}
void Operation3() {
//...
state_->Operation3();
state_ = state_->pNext;
//...
}
};
要点总结
State模式将所有与一个特定状态的相关的行为都放入一个State的子类对象中,在对象状态切换时,切换相应的对象;但同时维持State的接口,这样实现了具体操作与状态转换之间的解耦。
为不同的状态引入不同的对象使得状态转换变得更加明确,而且可怀疑保证不会出现状态不一致的情况,因为转换是原子性的---即要么彻底转换过来,要么不转换。
如果State对象没有实例变量,那么各个上下文可怀疑共享同一个State对象,从而节省对象开销。