2.2 设计模式之状态机模式

状态机模式

# 有限状态机

// 状态机模式
class state;
class transform;

// 事件有需要可以定义为类,用来传参
typedef int _event;

// 转移类
class transform
{
public:
	transform(_event evt,state* status):event_(evt),state_(status) {}
	~transform() {}

public:
	_event event_;
	state* state_;
};


// 状态类
class state
{
public:
	state() {}
	~state() {}

public:
	virtual void enterAction(void* data) {}
	virtual void transAction(void* data) {}
	virtual void exitAction(void* data) {}
public:
	bool addState(_event evt,state* status) {
		bool ret = false;
		if (getState(evt) == NULL) {
			transforms.push_back(new transform(evt, status));
			ret = true;
		}
		return ret;
	}
	state* getState(_event evt) {
		for (size_t i = 0;i < transforms.size();++i) {
			if (transforms[i]->event_ == evt) {
				return transforms[i]->state_;
			}
		}
		return NULL;
	}
protected:
	vector transforms;
};

// 状态机类
class stateMachine
{
public:
	stateMachine() {
		errorState_ = NULL;
		lastState_ = NULL;
		curState_ = NULL;
	}
	~stateMachine() {
		if (curState_ != NULL) {
			curState_->exitAction(NULL);
		}
		for (size_t i = 0;i < allState.size();++i) {
			delete allState[i];
		}
		allState.clear();
	}
public:
	void transfer(_event evt, void* data) {
		if (errorState_ == NULL) {
			return;
		}
	
		if (curState_ == NULL) {
			errorState_->enterAction(data);
			lastState_ = curState_;
			curState_ = errorState_;
			return;
		}

		if (curState_->getState(evt) == NULL) {
			curState_->exitAction(data);
			errorState_->exitAction(data);
			lastState_ = curState_;
			curState_ = errorState_;
			return;
		}
		
		if (curState_ == curState_->getState(evt)) {
			curState_->transAction(data);
			return;
		}

		curState_->exitAction(data);
		lastState_ = curState_;
		curState_ = curState_->getState(evt);
		curState_->enterAction(data);
	}


public:
	void setStartState(state* status) {
		if (status != NULL) {
			status->enterAction(NULL);
			curState_ = status;
		}
	}

	void setErrorState(state* status) {
		if (status != NULL) {
			errorState_ = status;
		}
	}

public:
	void addState(state* status) {
		allState.push_back(status);
	}

private:
	state* errorState_;
	state* lastState_;
	state* curState_;

private:
	vector allState;
	
};

// concrete state
class errorState : public state
{
public:
	errorState() {}
	~errorState() {}

public:
	virtual void enterAction(void* data) {
		cout << "enter error state" << endl;
	}
	virtual void transAction(void* data) {
		cout << "tran error state" << endl;
	}
	virtual void exitAction(void* data) {
		cout << "exit error state" << endl;
	}
};

class oneState : public state
{
public:
	oneState() {}
	~oneState() {}

public:
	virtual void enterAction(void* data) {
		cout << "enter one state" << endl;
	}
	virtual void transAction(void* data) {
		cout << "trans one state" << endl;
	}
	virtual void exitAction(void* data) {
		cout << "exit one state" << endl;
	}
};

class twoState : public state
{
public:
	twoState() {}
	~twoState() {}

public:
	virtual void enterAction(void* data) {
		cout << "enter two state" << endl;
	}
	virtual void transAction(void* data) {
		cout << "trans two state" << endl;
	}
	virtual void exitAction(void* data) {
		cout << "exit two state" << endl;
	}
};

class threeState : public state
{
public:
	threeState() {}
	~threeState() {}

public:
	virtual void enterAction(void* data) {
		cout << "enter three state" << endl;
	}
	virtual void transAction(void* data) {
		cout << "trans three state" << endl;
	}
	virtual void exitAction(void* data) {
		cout << "exit three state" << endl;
	}
};

class fourState : public state
{
public:
	fourState() {}
	~fourState() {}

public:
	virtual void enterAction(void* data) {
		cout << "enter four state" << endl;
	}
	virtual void transAction(void* data) {
		cout << "trans four state" << endl;
	}
	virtual void exitAction(void* data) {
		cout << "exit four state" << endl;
	}
};

void test_stateMachine(){
stateMachine machine;
	
	errorState* errorS = new errorState();
	oneState* startS = new oneState();
	twoState* twoS = new twoState();
	threeState* threeS = new threeState();
	fourState* fourS = new fourState();
	
	// 组织状态机
	machine.setErrorState(errorS);
	machine.setStartState(startS);
	
	// 保存所有状态  用来释放
	machine.addState(startS);
	machine.addState(twoS);
	machine.addState(threeS);
	machine.addState(fourS);
	machine.addState(errorS);
	
	// startS状态可以转移到  2,3 状态
	startS->addState(2, twoS);
	startS->addState(3, threeS);

	// twoS状态可以转移到  3,4 状态
	twoS->addState(3, threeS);
	twoS->addState(4, fourS);
	
	// threeS状态可以转移到 4 状态
	threeS->addState(4, fourS);

	// fourS状态可以转移到 1 4  状态
	fourS->addState(1, startS);
	fourS->addState(4, fourS);

	//测试  输入2,3,4,4,1
	_event evts[5] = { 2,3,4,4,1 };
	for (int i = 0;i < 5;++i) {
		machine.transfer(evts[i],NULL);
	}
}

你可能感兴趣的:(c/c++)