81 lines
1.5 KiB
C++
81 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <memory>
|
|
|
|
|
|
using namespace std;
|
|
|
|
class Request {
|
|
public:
|
|
int days;
|
|
enum {
|
|
PERSONAL_LEAVE,
|
|
ANNUAL_LEAVE,
|
|
} reason;
|
|
};
|
|
|
|
|
|
class Handler {
|
|
shared_ptr<Handler> next;
|
|
public:
|
|
void next_handle(const Request& r) {
|
|
if (!next) {
|
|
cout << "Request unhandled" << endl;
|
|
}
|
|
next->handle(r);
|
|
}
|
|
|
|
void set_next(shared_ptr<Handler> n) {
|
|
next = n;
|
|
}
|
|
|
|
virtual void handle(const Request& r) = 0;
|
|
};
|
|
|
|
|
|
class LeaderHandler : public Handler {
|
|
public:
|
|
virtual void handle(const Request& r) override {
|
|
if (r.days < 3) {
|
|
cout << "Leader: OK" << endl;
|
|
} else {
|
|
next_handle(r);
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
class SupervisorHandler : public Handler {
|
|
public:
|
|
virtual void handle(const Request& r) override {
|
|
if (r.days < 7 || r.reason == Request::ANNUAL_LEAVE) {
|
|
cout << "Supervisor: OK" << endl;
|
|
} else {
|
|
next_handle(r);
|
|
}
|
|
}
|
|
};
|
|
|
|
class ManagerHandler : public Handler {
|
|
public:
|
|
virtual void handle(const Request& r) override {
|
|
cout << "Manager: OK" << endl;
|
|
}
|
|
};
|
|
|
|
int main() {
|
|
auto lh = make_shared<LeaderHandler>();
|
|
auto sh = make_shared<SupervisorHandler>();
|
|
auto mh = make_shared<ManagerHandler>();
|
|
|
|
lh->set_next(sh);
|
|
sh->set_next(mh);
|
|
|
|
Request r1 {1, Request::PERSONAL_LEAVE};
|
|
Request r2 {4, Request::ANNUAL_LEAVE};
|
|
Request r3 {10, Request::PERSONAL_LEAVE};
|
|
|
|
lh->handle(r1);
|
|
lh->handle(r2);
|
|
lh->handle(r3);
|
|
}
|