Реализации на С++
Chain of Responsibility
Общая реализация на языке С++
class AbstractHandler
{
using PtrAbstractHandler = shared_ptr<AbstractHandler>;
protected:
PtrAbstractHandler next;
virtual bool run() = 0;
public:
using Default = shared_ptr<AbstractHandler>;
virtual ~AbstractHandler() = default;
virtual bool handle() = 0;
void add(PtrAbstractHandler node);
void add(PtrAbstractHandler node1, PtrAbstractHandler node2, ...);
void add(initializer_list<PtrAbstractHandler> list);
};class ConcreteHandler : public AbstractHandler
{
private:
bool condition
{
false
};
protected:
virtual bool run() override
{
cout << "Method run;" << endl;
return true;
}
public:
ConcreteHandler() : ConcreteHandler(false) {}
ConcreteHandler(bool c) : condition(c)
{
cout << "Constructor;" << endl;
}
virtual ~ConcreteHandler() override
{
cout << "Destructor;" << endl;
}
virtual bool handle() override
{
if (!condition) return next ? next->handle() : false;
return run();
}
};Last updated