Java教程

状态模式

本文主要是介绍状态模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

理论

 状态模式(State),当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂的情况。把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。

状态模式的优点:

将与特定状态相关的行为局部化,并且将不同状态的行为分割开。即将特定的状态的行为都放入一个对象中,由于所有与状态相关的代码都存在于某个 ConcreteState 中,所以通过定义新的子类可以很容易地增加新的状态和转换。

可以消除庞大地条件分支语句。通过把各种状态转移逻辑分布到 State 的子类之间,来减少相互间的依赖。

状态模式的应用场景:

当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式。

实例

 工作状态变化:上午上班,中午睡觉,下午渐恢复,加班苦煎熬(随着时间的变化改变状态)

初步实现

//工作状态变化:上午上班,中午睡觉,下午渐恢复,加班苦煎熬
#include <iostream>
using namespace std;

//工作类
class Work {
public:
	void SetHour(double _hour) {
		hour = _hour;
	}
	void SetFinish(bool _finish) {
		finish = _finish;
	}

	void WriteProgram() {
		if (hour < 12) {
			cout << "当前时间:" << hour << "点\t 上午上班,精神百倍" << endl;
		}
		else if (hour < 13) {
			cout << "当前时间:" << hour << "点\t 饿了,午饭:犯困,午休" << endl;
		}
		else if (hour < 17) {
			cout << "当前时间:" << hour << "点\t 下午状态还不错,继续努力" << endl;
		}
		else {
			if (finish) {
				cout << "当前时间:" << hour << "点\t 下班回家" << endl;
			}
			else {
				if (hour < 21) {
					cout << "当前时间:" << hour << "点\t 加班,疲累" << endl;
				}
				else {
					cout << "当前时间:" << hour << "点\t 不行了,睡着了" << endl;
				}
			}
		}
	}

private:
	double hour = 0.0;
	bool finish = false;
};

//客户端
int main()
{
	Work* emergencyProjects = new Work();
	emergencyProjects->SetHour(9);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(10);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(12);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(13);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(14);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(17);
	emergencyProjects->WriteProgram();

	emergencyProjects->SetFinish(false);

	emergencyProjects->SetHour(19);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(22);
	emergencyProjects->WriteProgram();

	delete emergencyProjects;

	system("pause");
	return 0;
}

状态模式

UML类图

代码实现

#include <iostream>
using namespace std;

class Work;

//抽象状态
class State {
public:
	virtual void WriteProgram(Work* w) {};
};

//具体工作状态类
class ForenoonState :public State {
public:
	virtual void WriteProgram(Work* w);
};
class NoonState :public State {
public:
	virtual void WriteProgram(Work* w);
};
class AfternoonState :public State {
public:
	virtual void WriteProgram(Work* w);
};
class EveningState :public State {
public:
	virtual void WriteProgram(Work* w);
};
class SleepingState :public State {
public:
	virtual void WriteProgram(Work* w);
};
class RestState :public State {
public:
	virtual void WriteProgram(Work* w);
};

//工作类
class Work{
public:
	Work(){
		current = new ForenoonState();
	}
	void SetHour(double _hour) {
		hour = _hour;
	}
	double GetHour() {
		return hour;
	}
	void SetFinish(bool _finish) {
		finish = _finish;
	}
	bool GetFinish() {
		return finish;
	}
	void SetState(State* s) {
		current = s;
	}
	void WriteProgram() {
		current->WriteProgram(this);
	}

private:
	State* current;
	double hour = 0.0;
	bool finish = false;
};

void ForenoonState::WriteProgram(Work* w) {
	if (w->GetHour() < 12) {
		cout << "当前时间:" << w->GetHour() << "点\t 上午上班,精神百倍" << endl;
	}
	else {
		w->SetState(new NoonState());
		w->WriteProgram();
	}
}
void NoonState::WriteProgram(Work* w) {
	if (w->GetHour() < 13) {
		cout << "当前时间:" << w->GetHour() << "点\t 饿了,午饭:犯困,午休" << endl;
	}
	else {
		w->SetState(new AfternoonState());
		w->WriteProgram();
	}
}
void AfternoonState::WriteProgram(Work* w) {
	if (w->GetHour() < 17) {
		cout << "当前时间:" << w->GetHour() << "点\t 下午状态还不错,继续努力" << endl;
	}
	else {
		w->SetState(new EveningState());
		w->WriteProgram();
	}
}
void EveningState::WriteProgram(Work* w) {
	if (w->GetFinish()) {
		w->SetState(new RestState());
		w->WriteProgram();
	}
	else {
		if (w->GetHour() < 21) {
			cout << "当前时间:" << w->GetHour() << "点\t 加班,疲累" << endl;
		}
		else {
			w->SetState(new SleepingState());
			w->WriteProgram();
		}
	}
}
void SleepingState::WriteProgram(Work* w) {
	cout << "当前时间:" << w->GetHour() << "点\t 不行了,睡着了" << endl;
}
void RestState::WriteProgram(Work* w) {
	cout << "当前时间:" << w->GetHour() << "点\t 下班回家" << endl;
}

//客户端
int main()
{
	Work* emergencyProjects = new Work();
	emergencyProjects->SetHour(9);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(10);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(12);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(13);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(14);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(17);
	emergencyProjects->WriteProgram();

	emergencyProjects->SetFinish(false);

	emergencyProjects->SetHour(19);
	emergencyProjects->WriteProgram();
	emergencyProjects->SetHour(22);
	emergencyProjects->WriteProgram();

	delete emergencyProjects;

	system("pause");
	return 0;
}

 

这篇关于状态模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!