Chain of Responsibility (C++实现)

// Chain of Responsibility.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;
typedef int Topic;

class Handler
{
public:
  Handler(Handler* h= 0, Topic t=-1):successor(h),topic(t)
  {
  } 

virtual ~Handler()
 {
 }

virtual  void HandleRequest(Topic t)=0;
 
 
protected:
   Handler * successor;
   Topic topic;
};

//////////////////////////////////////////////////////

class ConcreteHandler:public Handler
{
public:
 ConcreteHandler(Handler * h=0,Topic t=-1):Handler(h,t)
 {
 }
 virtual ~ConcreteHandler(){}

 virtual void HandleRequest(Topic t)
 {
  if(t==topic)
  {
   cout<<"Done this Request:"<<t<<endl;
   return;
  }
  else
  {
   if(NULL!=successor)
      {
     cout<<"Throw to successor"<<endl;
     successor->HandleRequest(t);
     
     }
  }
   
 }
 
};

int _tmain(int argc, _TCHAR* argv[])
{
 Handler *p1=new ConcreteHandler(0,3);
 Handler *p2=new ConcreteHandler(p1,2);
 Handler *p3=new ConcreteHandler(p2,3);

 Handler *px=new ConcreteHandler(p3);
 //px请求Topic为2.
 px->HandleRequest(2);

 delete p1;
 delete p2;
 delete p3;
 delete px;
 
 return 0;
}

 

原文链接: https://www.cnblogs.com/ustc11wj/archive/2011/05/27/2610906.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Chain of Responsibility (C++实现)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26269

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午3:58
下一篇 2023年2月8日 上午3:58

相关推荐