DesignPatterns_ChainOfResponsibility

本文介绍了一种设计模式——责任链模式,该模式通过构建对象链并沿着链传递请求,使得多个对象都有机会处理请求,从而避免了发送者与接收者之间的紧耦合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

///////////////////////////////////////////////////////////////////////////////
// Chain of Responsibility pattern
// - Avoid coupling the sender of a request to its receiver by giving more than
//   one object a chance to handle the request. Chain the receiving objects and
//   pass the request along the chain until an object handles it.
//
// Author     : ZAsia
// Date       : 15/05/12
// Warning    : In practice, declaration and implementation should
//              be separated(.h and .cpp).
////////////////////////////////////////////////////////////////////////////////
#include 
using namespace std;

#define SAFE_DELETE_PTR(ptr) \
{ if (ptr) { delete ptr; ptr = nullptr; } }

enum RequestType
{
	REQUEST1,
	REQUEST2,
	REQUEST3
};

// Handler
// - defines an interface for handling request.
// - (optional)implements the successor link.
class Handler
{
public:
	Handler(Handler *s) : _successor(s) { }
	~Handler() { }
	 
	virtual void HandleRequest(RequestType r)
	{
		if (_successor)
		{
			_successor->HandleRequest(r);
		}
	}

private:
	Handler *_successor;
};

// ConcreteHandler
// - handles requests it is responsible for.
// - can access its successor.
// - if the ConcreteHandler can handle the request, it does so; otherwise it
//   forwards the request to its successor.
class ConcreteHandler1 : public Handler
{
public:
	ConcreteHandler1(Handler *s) : Handler(s) { }

	virtual void HandleRequest(RequestType r)
	{
		if (r == REQUEST1)
		{
			cout << "ConcreteHandler1::HandleRequest..." << endl;
		}
		else
		{
			Handler::HandleRequest(r);
		}
	}
};

class ConcreteHandler2 : public Handler
{
public:
	ConcreteHandler2(Handler *s) : Handler(s) { }

	virtual void HandleRequest(RequestType r)
	{
		if (r == REQUEST2)
		{
			cout << "ConcreteHandler2::HandleRequest..." << endl;
		}
		else
		{
			Handler::HandleRequest(r);
		}
	}
};

class ConcreteHandler3 : public Handler
{
public:
	ConcreteHandler3(Handler *s) : Handler(s) { }

	virtual void HandleRequest(RequestType r)
	{
		if (r == REQUEST3)
		{
			cout << "ConcreteHandler3::HandleRequest..." << endl;
		}
		else
		{
			Handler::HandleRequest(r);
		}
	}
};

// Client
// - intiates the request to a ConcreteHandler object on the chain.
//
// Collaborations
// - when a client issues a request, the request propagates along the chain until
//   ConcreteHandler object takes responsibility for handling it.
int main()
{
	ConcreteHandler3 *pConcreteHandler3 = new ConcreteHandler3(nullptr);
	ConcreteHandler2 *pConcreteHandler2 = new ConcreteHandler2(pConcreteHandler3);
	ConcreteHandler1 *pConcreteHandler1 = new ConcreteHandler1(pConcreteHandler2);
	Handler *pHandler = new Handler(pConcreteHandler1);
	pHandler->HandleRequest(REQUEST1);
	pHandler->HandleRequest(REQUEST2);
	pHandler->HandleRequest(REQUEST3);

	SAFE_DELETE_PTR(pConcreteHandler3);
	SAFE_DELETE_PTR(pConcreteHandler2);
	SAFE_DELETE_PTR(pConcreteHandler1);
	SAFE_DELETE_PTR(pHandler);

	return 0;
}

/////////////////////////////////////////////////////////////////
// 1. Reduced coupling. An object only has to know that a request will be handled
//    "appropriately". Instead of objects maintaining references to all candidate 
//    candidate receivers, they keep a single reference to their successor.
// 2. Receipt isn't guaranteed. Since a request has no explicit receiver, there's 
//    no guarantee it'll be handled--the request can fall off the end of the chain 
//    without ever being handled. A request can also go unhandled when the chain is
//    not configured properly.
// 3. Connecting successors. The Handler not only defines the interface for the request
//    but usually maintains the successor as well. That lets the handler provide a
//    default implementation of HandleRequest that forwards the request to the successor.
//    (if any). If a ConcreteHandler subclass isn't interested in the request, it
//    doesn't have to override the forwarding operation, since its default implementation
//    forwards unconditionally.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值