设计模式——责任链模式(C++实现)

责任链模式简介

                将请求的发送者和接收者进行解耦。为请求创建了一个接收者对象的链,并沿着这条链传递该请求,直到有一个对象处理它为止。属于行为型模式。

适用场景:

              1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。

              2、在不明确指定接收者的情况下,向多个对象中的一个提交一个请求

              3、可处理一个请求的对象集合应被动态指定。

采用菜鸟教程的例子,这里用C++实现

#include<iostream>
#include<string>

using namespace std;

//创建抽象的日志记录器AbstractLogger类。
class AbstractLogger
{
public:
	AbstractLogger();
	virtual ~AbstractLogger();
	static int INFO;
	static int DEBUG;
	static int ERROR;

	void setNextLogger(AbstractLogger *nextLogger);
	void logMessage(int level, string messager);

protected:
	int level;
	AbstractLogger *nextLogger;

	virtual void write(string message)=0;

};

int AbstractLogger::INFO=1;
int AbstractLogger::DEBUG=2;
int AbstractLogger::ERROR=3;

AbstractLogger::AbstractLogger()
{
	level = INFO;
	nextLogger = NULL;
}
AbstractLogger::~AbstractLogger()
{
	delete nextLogger;
	nextLogger = NULL;
}

void AbstractLogger::setNextLogger(AbstractLogger *nextLogger)
{
	this->nextLogger = nextLogger;
}

void AbstractLogger::logMessage(int level, string messager)
{
	if (this->level == level)
	{
		write(messager);
	}
	if (nextLogger != NULL)
	{
		nextLogger->logMessage(level, messager);
	}
}

//创建扩展了AbstractLogger类的实体ConsoleLogger类。
class ConsoleLogger :public AbstractLogger
{
public:
	ConsoleLogger(int level);
	~ConsoleLogger() {};

	void write(string message);
};

ConsoleLogger::ConsoleLogger(int level)
{
	this->level = level;
}

void ConsoleLogger::write(string message)
{
	cout << "ConsoleLogger: console is "<<message << endl;
}

//创建扩展了AbstractLogger类的实体ErrorLogger类。
class ErrorLogger :public AbstractLogger
{
public:
	ErrorLogger(int level);
	~ErrorLogger() {};

	void write(string message);
};

ErrorLogger::ErrorLogger(int level)
{
	this->level = level;
}

void ErrorLogger::write(string message)
{
	cout << "ErrorLogger: console is " << message << endl;
}

//创建扩展了AbstractLogger类的实体FileLogger类。
class FileLogger :public AbstractLogger
{
public:
	FileLogger(int level);
	~FileLogger() {};

	void write(string message);
};

FileLogger::FileLogger(int level)
{
	this->level = level;
}

void FileLogger::write(string message)
{
	cout << "FileLogger: console is " << message << endl;
}

//创建不同类型的记录器。赋予它们不同的错误级别,并在每个记录器中设置下一个记录器。每个记录器中的下一个记录器代表的是链的一部分。
class chainPatternDemo
{
public:
	chainPatternDemo() {};
	~chainPatternDemo() {};
	static AbstractLogger *getchainOfLoggers();
};

AbstractLogger *chainPatternDemo::getchainOfLoggers()
{
	AbstractLogger *terrorlogger = (AbstractLogger*)new ErrorLogger(AbstractLogger::ERROR);
	AbstractLogger *tconsolelogger = (AbstractLogger*)new ConsoleLogger(AbstractLogger::DEBUG);
	AbstractLogger *tfilelogger = (AbstractLogger*)new FileLogger(AbstractLogger::INFO);

	terrorlogger->setNextLogger(tfilelogger);
	tfilelogger->setNextLogger(tconsolelogger);
	return terrorlogger;
}

int main()
{
	AbstractLogger *loggerchain = chainPatternDemo::getchainOfLoggers();
	loggerchain->logMessage(AbstractLogger::INFO, "This is an info information");
	loggerchain->logMessage(AbstractLogger::DEBUG, "This is an debug information");
	loggerchain->logMessage(AbstractLogger::ERROR, "This is an error information");
	
	delete loggerchain;
	loggerchain = NULL;
	system("pause");
	return 0;
}

在visual studio 2015上运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值