Chain of Responsibility模式
来源:互联网 发布:手机英语新闻软件 编辑:程序博客网 时间:2024/05/22 11:51
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;
}
- Chain of responsibility模式
- Chain of Responsibility模式
- Chain Of Responsibility模式
- Chain Of Responsibility模式
- Chain of Responsibility 模式
- Chain of Responsibility模式
- Chain of Responsibility 模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- Chain of Responsibility模式
- 模式中的chain of responsibility
- Chain of Responsibility设计模式
- Chain of Responsibility设计模式
- Chain Of Responsibility-----设计模式
- 设计模式之 Chain of Responsibility 模式
- 设计模式之Chain of Responsibility模式
- effective C++读书笔记 条款二 以编译器替换预处理器
- 10 条提升 Android 性能的建议
- 什么是Hibernate框架以及ORM
- ExtJs控件属性配置详细
- shapefile与字符集编码设置
- Chain of Responsibility模式
- Unity3D 调用Android原生方法2
- java猜数字小游戏:任意输入数字,判断与系统随机产生数字是否相等。
- 【框架】屏幕适配Autolayout的运用
- gdb的基本使用命令清单
- 理解伪元素 :before 和 :after
- IO模型的简单记录select、poll、epoll
- C/C++引用与指针
- test