#include #include "EventHandler.h" using namespace std; const Event LINK_1_BROKEN = 1; //Set up a chain of event handlers EventHandler::EventHandler ( EventHandler* h, Event t ) : _successor(h), _event(t) { } const bool EventHandler::HasEventSupport() { return _event != No_Event_Support; } void EventHandler::SetHandler(EventHandler* h, Event t) { _successor = h; _event = t; } void EventHandler::HandleEvent () { if (_successor != 0) { cout << "HandleEvent () calling into successor\n"; _successor->HandleEvent(); } } class Connection : public EventHandler { public: Connection(Event t) : EventHandler(0, t) { } virtual void HandleEvent(); }; void Connection ::HandleEvent() { } class Path : public Connection { public: Path::Path(Connection* w, Event t) : Connection(0) { cout << "In the Path constructor\n"; SetHandler(w, t); } void HandleEvent() { if (HasEventSupport()) { // Offer event support cout << "Here's some Path Event support"; } else { cout << "No support from Path - Sorry!\n"; cout << "Calling the base class event handler\n"; EventHandler::HandleEvent(); } } }; class Lsp : public Connection { public: Lsp::Lsp(char* lspID, Connection* w, Event t) : Connection(0) { cout << "Constructing Lsp: " << lspID << "\n"; lspName = lspID; SetHandler(w, t); } void HandleEvent() { if (HasEventSupport()) { // Offer event support cout << "\nAt last, here's some LSP Event support:\n"; cout << "We need a new path for " << lspName << "\n\n"; } else EventHandler::HandleEvent(); } private: char* lspName; }; void main() { Connection* connection = new Connection(No_Event_Support); Lsp* lsp = new Lsp("LSP123", connection, LINK_1_BROKEN); Path* path = new Path(lsp, No_Event_Support); path->HandleEvent(); }