普通状态机的实现
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>
using namespace std;
// 状态机状态的枚举
enum class State {
Idle,
Active,
Suspended,
Downloading,
Uploading,
Error
};
// 事件的枚举
enum class Event {
Start,
Stop,
Suspend,
Resume,
DownloadStart,
DownloadComplete,
UploadStart,
UploadComplete,
ErrorEncountered
};
// 状态机类
class StateMachine {
public:
StateMachine() : currentState(State::Idle) {}
// 处理事件的方法
void handleEvent(Event event) {
auto it = stateTransitionTable.find(make_pair(currentState, event));
if (it != stateTransitionTable.end()) {
// 执行状态转移和相应的操作
auto transition = it->second;
currentState = transition.first;
transition.second(); // 执行与状态转移相关的操作
cout << "Transition: " << stateToString(currentState) << endl;
} else {
cout << "Invalid transition from " << stateToString(currentState) << " on event " << eventToString(event) << endl;
}
}
private:
State currentState;
// 状态转移表,使用unordered_map存储当前状态和事件到下一状态及其对应的处理函数
unordered_map<pair<State, Event>, pair<State, function<void()>>> stateTransitionTable {
{ {State::Idle, Event::Start}, {State::Active, [](){ cout << "Machine started." << endl; }} },
{ {State::Idle, Event::ErrorEncountered}, {State::Error, [](){ cout << "Error encountered. Transition to Error state." << endl; }} },
{ {State::Active, Event::Stop}, {State::Idle, [](){ cout << "Machine stopped." << endl; }} },
{ {State::Active, Event::Suspend}, {State::Suspended, [](){ cout << "Machine suspended." << endl; }} },
{ {State::Suspended, Event::Resume}, {State::Active, [](){ cout << "Machine resumed." << endl; }} },
{ {State::Active, Event::DownloadStart}, {State::Downloading, [](){ cout << "Downloading started." << endl; }} },
{ {State::Downloading, Event::DownloadComplete}, {State::Active, [](){ cout << "Downloading completed." << endl; }} },
{ {State::Active, Event::UploadStart}, {State::Uploading, [](){ cout << "Uploading started." << endl; }} },
{ {State::Uploading, Event::UploadComplete}, {State::Active, [](){ cout << "Uploading completed." << endl; }} }
// 可根据需求继续添加更多状态和事件的转移规则
};
// 辅助方法,将状态和事件转换为字符串以便输出
string stateToString(State state) {
switch (state) {
case State::Idle: return "Idle";
case State::Active: return "Active";
case State::Suspended: return "Suspended";
case State::Downloading: return "Downloading";
case State::Uploading: return "Uploading";
case State::Error: return "Error";
default: return "Unknown";
}
}
string eventToString(Event event) {
switch (event) {
case Event::Start: return "Start";
case Event::Stop: return "Stop";
case Event::Suspend: return "Suspend";
case Event::Resume: return "Resume";
case Event::DownloadStart: return "DownloadStart";
case Event::DownloadComplete: return "DownloadComplete";
case Event::UploadStart: return "UploadStart";
case Event::UploadComplete: return "UploadComplete";
case Event::ErrorEncountered: return "ErrorEncountered";
default: return "Unknown";
}
}
};
int main() {
StateMachine machine;
// 通过不同的事件触发状态机的状态变化和相关操作
machine.handleEvent(Event::Start);
machine.handleEvent(Event::DownloadStart);
machine.handleEvent(Event::DownloadComplete);
machine.handleEvent(Event::UploadStart);
machine.handleEvent(Event::UploadComplete);
machine.handleEvent(Event::Suspend);
machine.handleEvent(Event::Resume);
machine.handleEvent(Event::Stop);
machine.handleEvent(Event::ErrorEncountered);
return 0;
}
状态模式的状态机的实现
个人认为非常不实用,开销大,不简洁,个人理解也就是易扩展这个优点,并没有感知到其他的优势。
除了那种真的有大几十种上百种的状态,也许会考虑下这种方式
理论性大于实用性