“状态变化”模式

  • 在组件构建过程中,某些对象的状态经常面临变化,如何对这些变化进行有效的管理?同时又维持高层模块的稳定?“状态变化"模式为这一问题提供了一种解决方案。
  • 典型模式
  1. State
  2. Memento

动机(Motivation)

  • 在软件构建过程中,某些对象的状态如果改变,其行为也会随之而发生变化,比如文档处于只读状态,其支持的行为和读写状态支持的行为就可能完全不同。
  • 如何在运行时根据对象的状态来透明地更改对象的行为?而不会为对象操作和状态转化之间引入紧耦合? //透明的:稳定

// 和策略模式很像。

示例

一般的写法,很多 if else, 和策略模式改造之前很像。

#include <iostream>
#include <string>

using namespace std;

enum NetworkState
{
Network_Open,
Network_Close,
Network_Connect,
};

class NetworkProcessor {
NetworkState state = NetworkState::Network_Open;
public:
/*很多 if else 和策略模式改造前很像。*/
void Operation1() {
if (state == Network_Open) {
//**********
cout << "网络" << "打开" << endl;
state = Network_Close;
}
else if (state == Network_Close) {
cout << "网络" << "关闭" << endl;
//..........
state = Network_Connect;
}
else if (state == Network_Connect) {
cout << "网络" << "连接" << endl;
//$$$$$$$$$$
state = Network_Open;
}
else;
}

void Operation2() {
if (state == Network_Open) {
//**********
cout << "网络" << "打开" << endl;
state = Network_Connect;
}
else if (state == Network_Close) {
//.....
cout << "网络" << "关闭" << endl;
state = Network_Open;
}
else if (state == Network_Connect) {
//$$$$$$$$$$
cout << "网络" << "连接" << endl;
state = Network_Close;
}
else;
}

void Operation3() {
//******
}
};

int main()
{
NetworkProcessor processor;
processor.Operation1();
processor.Operation1();
processor.Operation1();
processor.Operation1();

getchar();
return 0;
}

输出:

网络打开
网络关闭
网络连接
网络打开

状态模式:

#include <iostream>
#include <string>

using namespace std;

class NetworkState {
public:
NetworkState* pNext;
virtual void Operation1() = 0;
virtual void Operation2() = 0;
virtual void Operation3() = 0;
virtual ~NetworkState() {}
};

class CloseState :public NetworkState {
public:
CloseState() {};
~CloseState() {};
public:
static NetworkState* getInstance();
virtual void Operation1();
virtual void Operation2();
virtual void Operation3();
private:
// 这里用单例
static NetworkState* m_instance;
};

//后面扩展这个类就行
class OpenState :public NetworkState {
public:
OpenState() {};
~OpenState() {};
public:
static NetworkState* getInstance();
virtual void Operation1();
virtual void Operation2();
virtual void Operation3();
private:
// 这里用单例
static NetworkState* m_instance;
};

class ConnectState :public NetworkState {
public:
ConnectState() {};
~ConnectState() {};
public:
static NetworkState* getInstance();
virtual void Operation1();
virtual void Operation2();
virtual void Operation3();
private:
// 这里用单例
static NetworkState* m_instance;
};

NetworkState* OpenState::m_instance = nullptr;

NetworkState* OpenState::getInstance() {
if (m_instance == nullptr) {
m_instance = new OpenState();
}
return m_instance;
}

void OpenState::Operation1() {
//**********
cout << "OpenState " << "Operation1." << endl;
pNext = ConnectState::getInstance();
}

void OpenState::Operation2() {
cout << "OpenState " << "Operation2." << endl;
//..........
pNext = ConnectState::getInstance();
}

void OpenState::Operation3() {
cout << "OpenState " << "Operation3." << endl;
//$$$$$$$$$$
pNext = OpenState::getInstance();
}


NetworkState* CloseState::m_instance = nullptr;

NetworkState* CloseState::getInstance() {
if (m_instance == nullptr) {
m_instance = new CloseState();
}
return m_instance;
}

void CloseState::Operation1() {
cout << "CloseState " << "Operation1." << endl;
//**********
pNext = OpenState::getInstance();
}

void CloseState::Operation2() {
cout << "CloseState " << "Operation2." << endl;
//..........
pNext = ConnectState::getInstance();
}

void CloseState::Operation3() {
cout << "CloseState " << "Operation3." << endl;
//$$$$$$$$$$
pNext = OpenState::getInstance();
}


NetworkState* ConnectState::m_instance = nullptr;

NetworkState* ConnectState::getInstance() {
if (m_instance == nullptr) {
m_instance = new ConnectState();
}
return m_instance;
}

void ConnectState::Operation1() {
//**********
cout << "ConnectState " << "Operation1." << endl;
pNext = CloseState::getInstance();
}

void ConnectState::Operation2() {
cout << "ConnectState " << "Operation2." << endl;
//..........
pNext = ConnectState::getInstance();
}

void ConnectState::Operation3() {
cout << "ConnectState " << "Operation3." << endl;
//$$$$$$$$$$
pNext = OpenState::getInstance();
}

// 这个写好以后不用变
class NetworkProcessor {
NetworkState* pState;
public:
NetworkProcessor(NetworkState* pState) {
this->pState = pState;
}

void Operation1() {
//...
pState->Operation1();
pState = pState->pNext;
//...
}

void Operation2() {
//...
pState->Operation2();
pState = pState->pNext;
//...
}

void Operation3() {
//...
pState->Operation3();
pState = pState->pNext;
//...
}
};

int main()
{
OpenState openState;
CloseState closeState;
ConnectState connectState;

NetworkProcessor netWorkProcessor(&openState);
netWorkProcessor.Operation1();
netWorkProcessor.Operation1();
netWorkProcessor.Operation1();
netWorkProcessor.Operation1();

getchar();
return 0;
}

输出:

OpenState Operation1.
ConnectState Operation1.
CloseState Operation1.
OpenState Operation1.

模式定义

允许一个对象在其内部状态改变时改变它的行为。从而使对象看起来似乎修改了其行为。——《设计模式》GoF

类图

状态模式  State_DesignPattern

要点总结

  • State模式将所有与一个特定状态相关的行为都放入一个State的子类对象中,在对象状态切换时,切换相应的对象;但同时维持State的接口,这样实现了具体操作与状态转换之间的解耦。
  • 为不同的状态引入不同的对象使得状态转换变得更加明确,而且可以保证不会出现状态不一致的情况,因为转换是原子性的——即要么彻底转换过来,要么不转换。
  • 如果State对象没有实例变量,那么各个上下文可以共享同一个State对象,从而节省对象开销。



参考:GeekBand