c++设计模式之状态模式 学习笔记

这几天按照计划要学习设计模式中的状态模式,不巧前几天学习过度,脑袋昏昏沉沉的,导致博客看了一大堆,越看越烦,索性不看了,自己按照之前残存的记忆大致梳理一遍。

这里不拽文,不说什么专业术语,就简单聊一聊思路。

百度了一下状态这个词,是这么解释的—人或事物表现出来的形态。大白话理解一下,以人为例:这个人在吃饭,这个人在睡觉,这个人在打游戏,这个人在上厕所… …

上面的都可以理解为状态。当然了,提到上面的“状态”,你立马就脑补出这种状态的动作(请脑补一下你是怎么吃饭/睡觉/打游戏/上厕所的呢),也就是行为,换句话说,状态对应着行为,不同的状态对应着不同的行为。

那什么是状态模式呢?肯定和上面有关联,因为编程来源于生活也服务于生活。那就以生活中的事物为例。先想一下,什么东西是编程控制,有很多状态,供我们切换使用呢?答案有很多,随便举一个:空调吧。(因为现在南方天气开始变炎热,准备用空调了,昨天晚上刚刚擦了一遍我那个空调)

空调常用状态(模式):制冷,制热,抽湿,送风。你想想自己怎么用它的,开机后,你手拿遥控器,一点击制冷,它就开始制冷;一点击制热,它停止制冷,开始制热(至于怎么停止并且转换的,咱不管);一点击送风,它又开始送风… 很方便。而这个过程可以这么描述:你通过遥控器控制空调切换它的模式(状态),让它做相应的行为。

以这个例子咱们直接上代码。(注:本来想简单描述的,但是因为一点失误,代码写的稍微复杂了一点,再加上我水平不高,有点乱,请耐心看)

#include <iostream>
#include <thread>
using namespace std;


class AirConditioner;
class State
{
public:
	virtual void handle(AirConditioner *myAirConditioner) = 0;
	virtual string getName() = 0;
protected:
	string m_name;
};
class AirConditioner
{
public:
	AirConditioner(State *iniState) :nowState(iniState)
	{

	}
	void operator()()
	{
		QiDong();
	}
	void QiDong()
	{
		nowState->handle(this);
	}
	double getTemperature()
	{
		cout << "通过温度传感器实时监测温度,目前温度为35.6度" << endl;
		return 35.6;
	}
	double getHumidity()
	{
		cout << "通过湿度传感器实时监测温度,目前湿度为90.2%" << endl;
		return 90.2;
	}
	void setState(State * wantState)
	{
		nowState = wantState;
		nowState->handle(this);
	}
	string getNowStateName()
	{
		return nowState->getName();
	}
	void ZhiLeng()
	{
		while (getNowStateName() == "制冷")
		{
			while (this->getTemperature() > 23)
			{
				if (getNowStateName() != "制冷")
					break;
				cout << "太热了,发动机不停的制冷" << endl;
			}
			while (this->getTemperature() < 25)
			{
				if (getNowStateName() != "制冷")
					break;
				cout << "待机一下,休息休息" << endl;
			}
		}
	}
	void ZhiRe()
	{
		while (getNowStateName() == "制热")
		{
			while (this->getTemperature() < 27)
			{
				if (getNowStateName() != "制热")
					break;
				cout << "太冷了,发动机不停的制热" << endl;
			}
			while (this->getTemperature() > 25)
			{
				if (getNowStateName() != "制热")
					break;
				cout << "待机一下,休息休息" << endl;
			}
		}

	}
	void ChuShi()
	{
		while (getNowStateName() == "抽湿")
		{
			while (this->getHumidity() > 60)
			{
				if (getNowStateName() != "抽湿")
					break;
				cout << "湿度太大了,发动机不停的除湿" << endl;
			}
			while (this->getHumidity() < 65)
			{
				if (getNowStateName() != "抽湿")
					break;
				cout << "待机一下,休息休息" << endl;
			}
		}
	}
	void SongFeng()
	{
		while (getNowStateName() == "送风")
		{
			if (getNowStateName() != "送风")
				break;
			cout << "从室外往外吹风,给你清新空气" << endl;
		}
	}
protected:
	State *nowState;
};

class ZhiLengState :public State
{
public:
	ZhiLengState(string name)
	{
		m_name = name;
	}
	string getName()
	{
		return m_name;
	}
	void handle(AirConditioner *myAirConditioner)
	{
		thread QiDongL(&AirConditioner::ZhiLeng, myAirConditioner);
		QiDongL.detach();
		//myAirConditioner->ZhiLeng();
	}
};
class ZhiReState :public State
{
public:
	ZhiReState(string name)
	{
		m_name = name;
	}
	string getName()
	{
		return m_name;
	}
	void handle(AirConditioner *myAirConditioner)
	{
		thread QiDongL(&AirConditioner::ZhiRe, myAirConditioner);
		QiDongL.detach();
	}
};
class ChouShiState :public State
{
public:
	ChouShiState(string name)
	{
		m_name = name;
	}
	string getName()
	{
		return m_name;
	}
	void handle(AirConditioner *myAirConditioner)
	{
		thread QiDongL(&AirConditioner::ChuShi, myAirConditioner);
		QiDongL.detach();
		//myAirConditioner->ChuShi();
	}
};
class SongFengState :public State
{
public:
	SongFengState(string name)
	{
		m_name = name;
	}
	string getName()
	{
		return m_name;
	}
	void handle(AirConditioner *myAirConditioner)
	{
		thread QiDongL(&AirConditioner::SongFeng, myAirConditioner);
		QiDongL.detach();
	}
};

void main()
{
	State *startState = new ZhiLengState("制冷");
	AirConditioner myConditioner(startState);
	myConditioner.QiDong();
	//thread QiDongL(&AirConditioner::QiDong, &myConditioner);
	for (int i = 0;i < 10;i++)
	{
		cout << "我让它制冷一会儿,再切换为抽湿" << endl;
		
	}

	myConditioner.setState(new ChouShiState("抽湿"));
	for (int i = 0;i < 10;i++)
	{
		cout << "我让它抽湿一会儿,再切换为送风" << endl;
	}
	myConditioner.setState(new SongFengState("送风"));
	for (int i = 0;i < 10;i++)
	{
		cout << "送风功率比较低,不怕费电,我保持全天候送风" << endl;
	}
	system("pause");
	return;
}

如果转载请标明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值