本文共 1181 字,大约阅读时间需要 3 分钟。
Type: Behavioral
#includeusing namespace std; typedef int Request; class Handler { public: Handler(Handler *pSuccessor = NULL):m_pSuccessor(pSuccessor){}; // 纯虚函数,由派生类实现 virtual void handleRequest(Request request) = 0; protected: Handler* m_pSuccessor; }; class ConcreateHandler1: public Handler { public: void handleRequest(Request request) { if(request > 0) { cout<<"ConcreateHandler1 handle request."< handleRequest(request); } } } }; class ConcreateHandler2: public Handler { public: ConcreateHandler2(Handler *pSuccessor = NULL){ m_pSuccessor = pSuccessor; } void handleRequest(Request request) { if(request > 10) { cout<<"ConcreateHandler2 handle request."< handleRequest(request); } } } }; int main() { Handler *p1 = new ConcreateHandler1(); Handler *p2 = new ConcreateHandler2(p1); Request request = 1; p2->handleRequest(request); request = 12; p2->handleRequest(request); system("pause"); return 0; }
转载地址:http://hnzlo.baihongyu.com/