[C++]状态模式

在状态模式(State Pattern)中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。

在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象。

C++代码如下:

// 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// 参考大话设计模式 - 状态模式

#include <iostream>

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
#endif

// 状态模式:存在多种状态,用if else逻辑较为繁琐时,可采用状态模式进行管控
// 
// 注意:内存泄露问题

using namespace std;

class Work;
class FornoonState;

class State {
public:
	virtual void writeProgram(Work* work) = 0;
};

class Work {
public:
	Work();
	~Work() {
		SAFE_DELETE(state_);
	}

	void setHour(int hour) {
		hour_ = hour;
	}
	int getHour() {
		return hour_;
	}

	void setFinish(bool finish) {
		finish_ = finish;
	}
	int getFinish() {
		return finish_;
	}

	// 此处注意内存泄露
	void setState(State* state) {
		SAFE_DELETE(state_);
		state_ = state;
	}

	void getCurrentState() {
		state_->writeProgram(this);
	}

private:
	int hour_ = 0;
	bool finish_ = false;
	State* state_ = nullptr;
};

class SleeptimeState : public State {
public:
	void writeProgram(Work* work) {
		cout << "time: " << work->getHour() << ", can not work ,need sleep" << endl;
	}
};

class GohomeState : public State {
public:
	void writeProgram(Work* work) {
		work->setFinish(true);
	}
};

class NigthtimeState : public State {
public:
	void writeProgram(Work* work) {
		if (work->getFinish()) {
			cout << "time: " << work->getHour() << ", work done, go home" << endl;
			work->setState(new GohomeState());
		}
		else {
			if (work->getHour() < 21) {
				cout << "time: " << work->getHour() << ", work very tired." << endl;
			}
			else {
				work->setState(new SleeptimeState());
				work->getCurrentState();
			}
		}
	}
};

class AfternoonState : public State {
public:
	void writeProgram(Work* work) {
		if (work->getHour() < 19) {
			cout << "time: " << work->getHour() << ", work tired." << endl;
		}
		else {
			work->setState(new NigthtimeState());
			work->getCurrentState();
		}
	}
};

class NoontimeState : public State {
public:
	void writeProgram(Work* work) {
		if (work->getHour() < 14) {
			cout << "time: " << work->getHour() << ", work hungry." << endl;
		}
		else {
			work->setState(new AfternoonState());
			work->getCurrentState();
		}
	}
};

class FornoonState : public State {
public:
	void writeProgram(Work* work) {
		if (work->getHour() < 12) {
			cout << "time: " << work->getHour() << ", work wonderful." << endl;
		} else {
			work->setState(new NoontimeState());
			work->getCurrentState();
		}
	}
};

Work::Work() {
	state_ = new FornoonState();
}

int main()
{
	Work* work = new Work();

	work->setHour(8);
	work->getCurrentState();

	work->setHour(9);
	work->getCurrentState();

	work->setHour(12);
	work->getCurrentState();

	work->setHour(15);
	work->getCurrentState();

	work->setHour(17);
	work->getCurrentState();

	work->setHour(19);
	work->getCurrentState();

	work->setFinish(true);
	work->getCurrentState();

	work->setHour(21);
	work->getCurrentState();

	SAFE_DELETE(work);

	return 0;
}


github源码路径:https://github.com/dangwei-90/Design-Mode

持续更新中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值