From ccb84d781876f34f30596f92ebaa5973574c13bd Mon Sep 17 00:00:00 2001 From: Jakub Vojvoda Date: Sun, 18 Sep 2016 15:00:15 +0200 Subject: [PATCH] add Chain of Responsibility --- .../ChainOfResponsibility.cpp | 87 +++++++++++++++++++ chain-of-reponsibility/README.md | 12 +++ 2 files changed, 99 insertions(+) create mode 100644 chain-of-reponsibility/ChainOfResponsibility.cpp create mode 100644 chain-of-reponsibility/README.md diff --git a/chain-of-reponsibility/ChainOfResponsibility.cpp b/chain-of-reponsibility/ChainOfResponsibility.cpp new file mode 100644 index 0000000..af15a40 --- /dev/null +++ b/chain-of-reponsibility/ChainOfResponsibility.cpp @@ -0,0 +1,87 @@ +/* + * C++ Design Patterns: Chain of Responsibility + * Author: Jakub Vojvoda [github.com/JakubVojvoda] + * 2016 + * + * Source code is licensed under MIT licence + * (for more details see LICENCE) + * + */ +#include + +/* + * Handler + * defines an interface for handling requests and + * optionally implements the successor link + */ +class Handler { +public: + virtual void setHandler(Handler *s) { + successor = s; + } + + virtual void handleRequest() { + if (successor != 0) { + successor->handleRequest(); + } + } + // ... + +private: + Handler *successor; +}; + +/* + * Concrete Handlers + * handle requests they are responsible for + */ +class ConcreteHandler1 : public Handler { +public: + bool canHandle() { + // ... + return false; + } + + virtual void handleRequest() { + if (canHandle()) { + std::cout << "Handled by Concrete Handler 1" << std::endl; + } else { + std::cout << "Cannot handle by Handler 1" << std::endl; + Handler::handleRequest(); + } + // ... + } + // ... +}; + +class ConcreteHandler2 : public Handler { +public: + bool canHandle() { + // ... + return true; + } + + virtual void handleRequest() { + if (canHandle()) { + std::cout << "Handled by Handler 2" << std::endl; + } else { + std::cout << "Cannot handle by Handler 2" << std::endl; + Handler::handleRequest(); + } + // ... + } + + // ... +}; + + +int main() +{ + ConcreteHandler1 handler1; + ConcreteHandler2 handler2; + + handler1.setHandler(&handler2); + handler1.handleRequest(); + + return 0; +} diff --git a/chain-of-reponsibility/README.md b/chain-of-reponsibility/README.md new file mode 100644 index 0000000..01dbdde --- /dev/null +++ b/chain-of-reponsibility/README.md @@ -0,0 +1,12 @@ +## Chain of Responsibility + +Chain of Responsibility pattern avoids coupling the sender of a request to its receiver +by giving more than one object a chance to handle the request. The pattern chains +the receiving objects and passes the request along the chain until an object handles it. +It has a behavioral purpose and deals with relationships between objects. + +### When to use + +* more than one object may handle a request, and the handler should be ascertained automatically +* you want to issue a request to one of several objects without specifying the receiver explicitly +* the set of objects that can handle a request should be specified dynamically \ No newline at end of file