///////////////////////////////////////////////////////////////////////////////
// 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.