责任链模式 和 状态模式

责任链模式 和 状态模式
共同点

核心逻辑, 都是在本类中, 包含另外一个能处理该逻辑的类, 如果本类无法处理, 就用另外一个类来处理

, 直至最终找到能够处理该逻辑的类,
不同点

有的说是责任链模式是客户端设置的下一级处理类, 状态模式是类中设置好的下个状态

其实根本逻辑都是差不多的, 只不过是下一级处理类的设置的阶段不通而已.

责任链模式

我们设置三个不同级别的身份

班长: 处理level <= 1的事务

教师: 处理level == 2的事务

校长: 处理所有级别的事务

然后在客户端( main方法里, 不是模式代码中 )分别设置他们的上一级处理人, 代码如下

cpp 复制代码
class Processor
{
public:
	Processor* p;
	void set_superior(Processor* _p) { p = _p; }
	virtual void process(int level) = 0;
};


class Monitor : public Processor
{
public:
	void process(int level) override
	{
		if (level <= 1)
			cout << "monitor can process this" << endl;
		else
			p->process(level);
	}
};


class Teacher : public Processor
{
public:
	void process(int level) override
	{
		if (level <= 2)
			cout << "teacher can process this" << endl;
		else
			p->process(level);
	}
};


class Headmaster : public Processor
{
public:
	void process(int level) override
	{
		cout << "Headmaster process this" << endl;
	}
};


int main()
{
	Monitor* m = new Monitor();
	Teacher* t = new Teacher();
	Headmaster* h = new Headmaster();
	m->set_superior(t);
	t->set_superior(h);
	m->process(3);
	return 0;
}

执行结果

状态模式

状态模式的状态切换, 都是状态内部设置的, 客户端不用管

例如工作状态, 分为

早上: 10点前

中午: 13点前

下午: 18点前

晚上: 18点后

它们之间的状态切换会默认执行, 如下所示

cpp 复制代码
class WorkState
{
public:
	virtual WorkState* work(int hour) = 0;
};



class NightState : public WorkState
{
public:
	WorkState* work(int hour) override
	{
		if (hour < 19)
		{
			cout << "time to dinner" << endl;
		}
		else
		{
			cout << "time to off work" << endl;
		}
		return this;
	}
};


class AfternoonState : public WorkState
{
public:
	WorkState* work(int hour) override
	{
		if (hour < 18)
		{
			cout << "afternoon time, good to work" << endl;
			return this;
		}
		else
		{
			WorkState* res = new NightState();
			res = res->work(hour);
			return res;
		}
	}
};


class NoonState : public WorkState
{
public:
	WorkState* work(int hour) override
	{
		if (hour < 13)
		{
			cout << "not time to noon sleep" << endl;
			return this;
		}
		else
		{
			WorkState* res = new AfternoonState();
			res = res->work(hour);
			return res;
		}
	}
};


class ForenoonState : public WorkState
{
public:
	WorkState* work(int hour) override
	{
		if (hour < 10)
		{
			cout << "not time to work" << endl;
			return this;
		}
		else if (hour < 12)
		{
			cout << "forenoon time to work" << endl;
			return this;
		}
		else
		{
			WorkState* res = new NoonState();
			res = res->work(hour);
			return res;
		}
	}
};



int main()
{
	WorkState* ws = new ForenoonState();

	ws = ws->work(7);
	ws = ws->work(11);
	ws = ws->work(12);
	ws = ws->work(15);
	ws = ws->work(19);

	return 0;
}

执行结果

相关推荐
Madison-No77 分钟前
【C++】探秘string的底层实现
开发语言·c++
无限进步_1 小时前
C语言字符串与内存操作函数完全指南
c语言·c++·算法
闻缺陷则喜何志丹1 小时前
【C++贪心】P10537 [APIO2024] 九月|普及+
c++·算法·贪心·洛谷
QiZhang | UESTC1 小时前
JAVA算法练习题day27
java·开发语言·c++·算法·leetcode·hot100
Stanford_11062 小时前
关于嵌入式硬件需要了解的基础知识
开发语言·c++·嵌入式硬件·微信小程序·微信公众平台·twitter·微信开放平台
是那盏灯塔3 小时前
16.C++三大重要特性之多态
开发语言·c++
我是华为OD~HR~栗栗呀3 小时前
24届-Python面经(华为OD)
java·前端·c++·python·华为od·华为·面试
胖咕噜的稞达鸭4 小时前
缝合怪deque如何综合list和vector实现及仿函数模板如何优化priority_queue实现
数据结构·c++·算法·链表·list
tt5555555555554 小时前
C++ 经典数组算法题解析与实现教程
开发语言·c++·算法
泽虞5 小时前
《Qt应用开发》笔记
linux·开发语言·c++·笔记·qt