Chain of Responsibility模式

Chain of Responsibility模式

一.    意图

使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止。

二.    适用性

1.  有多个的对象可以处理一个请求,那个对象处理该请求运行时刻自动确定。

2.  你想在不明确指定接受者的情况下,向多个对象中的一个提交一个请求。

3.  可处理一个请求的对象集合应被动态指定

三.    结构

 

 


四.    代码

#include<iostream>

#include<stdio.h>

#include<stdlib.h>

#include<vector>

#include<math.h>

 

usingnamespacestd;

//chainofresponsibilitymode

enumenFruitType

{

   FRUIT_TYPE_APPLE=0,

   FRUIT_TYPE_BANANA,

   FRUIT_TYPE_ORANGE

};

//抽象请求

classCAbstractRequest

{   

public:

   CAbstractRequest(enFruitTypetype)

   {

       m_fruitType=type;

   }

   ~CAbstractRequest()

   {}

 

   enFruitTypeGetFruitType()

   {

       returnm_fruitType;

   }

 

   virtualvoidGetFruitPrice(){}

 

private:

   enFruitTypem_fruitType;

};

//实际请求事件--1

classCRequestApple:publicCAbstractRequest

{

public:

   CRequestApple():CAbstractRequest(FRUIT_TYPE_APPLE){}

 

   voidGetFruitPrice()

   {

       cout<<"fruittypeappleprice=1.1"<<endl;

   }

};

//实际请求事件--2

classCRequestBanana:publicCAbstractRequest

{

public:

   CRequestBanana():CAbstractRequest(FRUIT_TYPE_BANANA){}

 

   voidGetFruitPrice()

   {

       cout<<"fruittypebananaprice=2.2"<<endl;

   }

};

//实际请求事件--3

classCRequestOrange:publicCAbstractRequest

{

public:

   CRequestOrange():CAbstractRequest(FRUIT_TYPE_ORANGE){}

 

   voidGetFruitPrice()

   {

       cout<<"fruittypeorangeprice=3.3"<<endl;

   }

};

 

//抽象处理类

classCAbstractHandler

{

public:

   CAbstractHandler()

   {

   }

   voidhandleRequest(CAbstractRequest*request)

   {

       if(request->GetFruitType()==GetHandleType())

       {

           request->GetFruitPrice();

           handle(request);

       }

       else

       {

           if(m_pNextHandler!=NULL)

           {

               m_pNextHandler->handleRequest(request);

           }

           else

           {

               cout<<"Error:--->out--->nohandler!"<<endl;

           }

       }

   }

 

   voidSetNextHandler(CAbstractHandler*handler)

   {

       m_pNextHandler=handler;

   }

 

   virtualenFruitTypeGetHandleType(){}

   virtualvoidhandle(CAbstractRequest*handler){}

private:

   CAbstractHandler*m_pNextHandler;

};

//处理事件--1

classCHandlerApple:publicCAbstractHandler

{

public:

   enFruitTypeGetHandleType()

   {

       returnFRUIT_TYPE_APPLE;

   }

   voidhandle(CAbstractRequest*handler)

   {

       cout<<"handlerappleevent"<<endl;

   }

};

//处理事件--2

classCHandlerBanana:publicCAbstractHandler

{

public:

   enFruitTypeGetHandleType()

   {

       returnFRUIT_TYPE_BANANA;

   }

   voidhandle(CAbstractRequest*handler)

   {

       cout<<"handlerbananaevent"<<endl;

   }

};

//处理事件--3

classCHandlerOrange:publicCAbstractHandler

{

public:

   enFruitTypeGetHandleType()

   {

       returnFRUIT_TYPE_ORANGE;

   }

   voidhandle(CAbstractRequest*handler)

   {

       cout<<"handlerorangeevent"<<endl;

   }

};

 

intmain()

{

   cout<<"HelloWorld!"<<endl;

   CAbstractHandler*handlerApple=newCHandlerApple();

   CAbstractHandler*handlerBanana=newCHandlerBanana();

   CAbstractHandler*handlerOrange=newCHandlerOrange();

   handlerApple->SetNextHandler(handlerBanana);

   handlerBanana->SetNextHandler(handlerOrange);

 

//   CAbstractRequest*reqOrange=newCRequestOrange();

   CAbstractRequest*reqBanana=newCRequestBanana();

   handlerApple->handleRequest(reqBanana);

 

   return0;

}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值