用代码和UML图化解设计模式之《责任链模式》
来源:互联网 发布:苟利国家什么梗 知乎 编辑:程序博客网 时间:2024/05/30 23:01
责任链模式,用我的理解就是动作的处理链表。根据请求的不同类型,在链表查找相应类型的处理者。处理者是一个链表。
wiki上说明
wikipedia的定义为:CoR pattern consists of a source of command objects and a series of processing objects. Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those that it cannot handle to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.
用下面的一个图来解释
基本的UML图为
// ChainofResponsibility.cpp : 定义控制台应用程序的入口点。//************************************************************************/ /* @filename ChainofResponsibility.cpp@author wallwind @createtime 2012/11/6 11:58@function 责任链模式@email wochenglin@qq.com @weibo @成林有理想*/ /************************************************************************/#include "stdafx.h"#include <iostream>using namespace std;class IChain{public:IChain():m_nextChain(NULL){}virtual ~IChain(){if (m_nextChain !=NULL){delete m_nextChain;m_nextChain = NULL;}}void setChainHandler(IChain* handler){m_nextChain = handler;}virtual void handleReq(int reqtype) = 0;protected:IChain* m_nextChain;};class FirstHandler:public IChain{public:FirstHandler():IChain(){}virtual ~FirstHandler(){}virtual void handleReq(int reqtype){if (reqtype <3){cout<<"FirstHandler::handleReq"<<endl;}else if (m_nextChain !=NULL){m_nextChain->handleReq(reqtype);}else{cout<<"no handler"<<endl;}}};class SecondHandler:public IChain{public:SecondHandler():IChain(){}virtual ~SecondHandler(){}virtual void handleReq(int reqtype){if (reqtype <7){cout<<"SecondHandler::handleReq"<<endl;}else if (m_nextChain !=NULL){m_nextChain->handleReq(reqtype);}else{cout<<"no handler"<<endl;}}};class ThirdHandler:public IChain{public:ThirdHandler():IChain(){}virtual ~ThirdHandler(){}virtual void handleReq(int reqtype){if (reqtype <9){cout<<"ThirdHandler::handleReq"<<endl;}else if (m_nextChain !=NULL){m_nextChain->handleReq(reqtype);}else{cout<<"no handler"<<endl;}}};int _tmain(int argc, _TCHAR* argv[]){IChain *p1handler= new FirstHandler;IChain *p2handler= new SecondHandler;IChain *p3handler= new ThirdHandler;p1handler ->setChainHandler(p2handler);p2handler->setChainHandler(p3handler);p3handler ->handleReq(4);delete p1handler,p2handler,p3handler;return 0;}
- 用代码和UML图化解设计模式之《责任链模式》
- 用代码和UML图化解设计模式之《策略模式》
- 用代码和UML图化解设计模式之《代理模式》
- 用代码和UML图化解设计模式之《工厂模式》
- 用代码和UML图化解设计模式之《抽象工厂模式》
- 用代码和UML图化解设计模式之《门面模式》
- 用代码和UML图化解设计模式之《适配器模式》
- 用代码和UML图化解设计模式之《模板方法模式》
- 用代码和UML图化解设计模式之《创建者模式》
- 用代码和UML图化解设计模式之《桥接模式》
- 用代码和UML图化解设计模式之《命令模式》
- 用代码和UML图化解设计模式之《装饰模式》
- 用代码和UML图化解设计模式之《迭代器模式》
- 用代码和UML图化解设计模式之《观察者模式》
- 责任链设计模式代码
- 设计模式-责任链模式(代码)
- 设计模式之责任链
- 设计模式之责任链
- 二分查找
- Linux进程
- POI读取Excel 各种特殊数字和类型的转换
- Java集合框架(一)之List详解
- Java多线程编程要点
- 用代码和UML图化解设计模式之《责任链模式》
- linux 常用命令
- 一个子窗口向父窗口赋值的例子
- 大端小端(Big- Endian和Little-Endian)
- 关于花生壳+IIS打造个人FTP服务器(未完待续)
- 艾呀喵啊 (特判与大数)
- 经典makefile例子
- Kinect for Windows SDK开发入门(二):基础知识 上
- VMWare虚拟机下为Ubuntu配置静态IP(NAT方式)