首页 > 其他 > 详细

State Design Pattern 状态设计模式2

时间:2014-08-02 18:27:44      阅读:284      评论:0      收藏:0      [点我收藏+]

其实本设计模式可以实现一些像人工智能式的问答。

这个跟人回答问题差不多,根据不同的问题作出不同的回答,而且在不同的状态,也会作出不同的回答。

如果有大量的数据,那么就可以作出千变万化的问答效果来哦。

#include <stdio.h>

enum STATE
{
	HUNGARY, EAT, FULL
};

class State
{
	STATE myState;
public:
	State(STATE s = HUNGARY) : myState(s)
	{

	}

	void eat()
	{
		switch (myState)
		{
		case State_2::HUNGARY:
			puts("Good, I am hungary now. I would like something to eat.");
			myState = EAT;
			break;
		case State_2::EAT:
			puts("I am still eating. I am not finished, yet.");
			break;
		case State_2::FULL:
			puts("Not, Thanks. I am full now.");
			break;
		default:
			break;
		}
	}

	void hungary()
	{
		switch (myState)
		{
		case State_2::HUNGARY:
			puts("I am starving! I need to eat something!");
			break;
		case State_2::EAT:
			puts("I am still hungary, I want to eat more.");
			break;
		case State_2::FULL:
			puts("Now, I am feeling hungary again.");
			myState = HUNGARY;
			break;
		default:
			break;
		}
	}

	void full()
	{
		switch (myState)
		{
		case State_2::HUNGARY:
			puts("I am not full, I need to eat something first.");
			break;
		case State_2::EAT:
			puts("Great, I am full now. Thank you.");
			myState = FULL;
			break;
		case State_2::FULL:
			puts("Yeah, can not be more satisfied. I am full, and full again.");
			break;
		default:
			break;
		}
	}
};

int main()
{
	State Bill;

	puts("\nWhen Bill is in HUNGARY state:");
	puts("Are you hungary, Bill?");
	Bill.hungary();
	puts("Are you full, Bill?");
	Bill.full();
	puts("Would you like something to eat, Bill?");
	Bill.eat();	//Change state

	puts("\nWhen Bill is in EAT state:");
	puts("Would you like something to eat, Bill?");
	Bill.eat();
	puts("Are you hungary, Bill?");
	Bill.hungary();
	puts("Are you full, Bill?");
	Bill.full();	//Change state

	puts("\nWhen Bill is in FULL state:");
	puts("Are you full, Bill?");
	Bill.full();
	puts("Would you like something to eat, Bill?");
	Bill.eat();
	puts("Are you hungary, Bill?");
	Bill.hungary();	//Change state

	return 0;
}

看看运行效果:

bubuko.com,布布扣


State Design Pattern 状态设计模式2,布布扣,bubuko.com

State Design Pattern 状态设计模式2

原文:http://blog.csdn.net/kenden23/article/details/38349327

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!