C++ 责任链模式

责任链模式

责任链模式的核心是解决一组服务中的先后执行处理关系。

在这里插入图片描述

#pragma once
#include <iostream>
#include <string>
class Auth
{
public:
	virtual void setSuperior(Auth* superior) = 0;
	virtual void handleRequest(double amount) = 0;
};

class Level1Auth : public Auth
{
public:
	Level1Auth(double ma, std::string n):maxAmount(ma),name(n),sup(nullptr){}
	void setSuperior(Auth* superior) { this->sup = superior; }
	void handleRequest(double amount)
	{
		if (amount <= maxAmount)
			return;
		else
		{
			if (this->sup != nullptr)
			{
				this->sup->handleRequest(amount);
				return;
			}
		}
	}
private:
	double maxAmount;
	std::string name;
	Auth* sup;
};

class GroupLeader : public Level1Auth
{
public:
	explicit GroupLeader(std::string name) :Level1Auth(100,name){}
};

class Manager : public Level1Auth
{
public:
	explicit

版权声明:本文为xiaobeibei2原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。