责任链模式 和 状态模式

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

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

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

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

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

责任链模式

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

班长: 处理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;
}

执行结果

相关推荐
影视飓风TIM1 小时前
C++哈希表深度剖析:冲突处理、rehash、迭代器与unordered容器封装
c++·哈希算法·散列表
C++ 老炮儿的技术栈1 小时前
Qt5 使用 QPainter 绘制阿基米德螺线
开发语言·c++·windows·qt·代码化
xxwxx__2 小时前
深入理解 C++ STL:stack、queue 与 deque 从使用到底层实现全解析
开发语言·c++·算法
wuminyu2 小时前
JVM锁膨胀与Futex源码解析
java·linux·c语言·jvm·c++
郝学胜-神的一滴2 小时前
C++11 工程级应用 08:Lambda表达式与Tuple元组
开发语言·jvm·c++·python·程序人生·开源
水饺编程3 小时前
第5章,[Win32 章节] :圆角矩形教学插图绘制程序
c语言·c++·windows·visual studio
Existence_Time3 小时前
CSP 考前复习
c++
zerwave4 小时前
从零实现简化 DDS 完整技术文档(含模块作用与详细注释)
linux·c++·机器人
Dream Cosmos4 小时前
C++ 多态下篇:虚函数表、动态绑定与多态底层原理
开发语言·c++
_Twink1e17 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen