1.Chain of Responsibility模式:将可能处理一个请求的对象链接成一个链,并将请求在这个链上传递,直到有对象处理该请求(可能需要提供一个默认处理所有请求的类,例如MFC中的CwinApp类)。

2.Chain of Responsibility Pattern 结构图

3.实现

 #ifndef _HANDLE_H_
#define _HANDLE_H_ class Handle
{
public:
virtual ~Handle();
virtual void HandleRequest() = ;
void SetSuccessor(Handle* succ);
Handle* GetSuccessor(); protected:
Handle();
Handle(Handle* succ); private:
Handle* _succ;
}; class ConcreteHandleA:public Handle
{
public:
ConcreteHandleA();
~ConcreteHandleA();
ConcreteHandleA(Handle* succ);
void HandleRequest(); protected:
private:
}; class ConcreteHandleB:public Handle
{
public:
ConcreteHandleB();
~ConcreteHandleB();
ConcreteHandleB(Handle* succ);
void HandleRequest();
protected:
private:
}; #endif

Handle.h

 #include "Handle.h"
#include <iostream>
using namespace std; Handle::Handle()
{
_succ = ;
}
Handle::~Handle()
{
delete _succ;
}
Handle::Handle(Handle* succ)
{
this->_succ = succ;
}
void Handle::SetSuccessor(Handle* succ)
{
_succ = succ;
}
Handle* Handle::GetSuccessor()
{
return _succ;
}
void Handle::HandleRequest()
{ }
ConcreteHandleA::ConcreteHandleA()
{ }
ConcreteHandleA::ConcreteHandleA(Handle* succ):Handle(succ)
{ }
ConcreteHandleA::~ConcreteHandleA()
{ }
void ConcreteHandleA::HandleRequest()
{
if (this->GetSuccessor() != )
{
cout<<"ConcreteHandleA 我把处理权给后继节点....."<<endl;
this->GetSuccessor()->HandleRequest();
}
else
{
cout<<"ConcreteHandleA 没有后继了,我必须自己处理...."<<endl;
}
}
ConcreteHandleB::ConcreteHandleB()
{ }
ConcreteHandleB::ConcreteHandleB(Handle* succ):Handle(succ)
{ }
ConcreteHandleB::~ConcreteHandleB()
{ }
void ConcreteHandleB::HandleRequest()
{
if (this->GetSuccessor() != )
{
cout<<"ConcreteHandleB 我把处理权给后继节点....."<<endl;
this->GetSuccessor()->HandleRequest();
}
else
{
cout<<"ConcreteHandleB 没有后继了,我必须自己处理...."<<endl;
}
}

Handle.cpp

 #include "Handle.h"
#include <iostream>
using namespace std; int main(int argc,char* argv[])
{
Handle* h1 = new ConcreteHandleA();
Handle* h2 = new ConcreteHandleB();
h1->SetSuccessor(h2);
h1->HandleRequest();
return ;
}

main.cpp

Chain of Responsibility Pattern的更多相关文章

  1. 深入浅出设计模式——职责链模式(Chain of Responsibility Pattern)

    模式动机 职责链可以是一条直线.一个环或者一个树形结构,最常见的职责链是直线型,即沿着一条单向的链来传递请求.链上的每一个对象都是请求处理者,职责链模式可以将请求的处理者组织成一条链,并使请求沿着链传 ...

  2. 二十四种设计模式:责任链模式(Chain of Responsibility Pattern)

    责任链模式(Chain of Responsibility Pattern) 介绍为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求.将这些对象连成一条链,并沿着这条链传递该请求,直 ...

  3. 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)

    原文:乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 责任链模式(Chain of R ...

  4. C#设计模式之二十一职责链模式(Chain of Responsibility Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中 ...

  5. 责任链模式 职责链模式 Chain of Responsibility Pattern 行为型 设计模式(十七)

    责任链模式(Chain of Responsibility Pattern) 职责链模式 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系 将这些对象连接成一条链,并沿着这 ...

  6. C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中的例子吧,理解起来可能更 ...

  7. 19.职责链模式(Chain of Responsibility Pattern)

    19.职责链模式(Chain of Responsibility Pattern)

  8. Design Patterns Uncovered: The Chain Of Responsibility Pattern

    Chain of Responsibility in the Real World The idea of the Chain Of Responsibility is that it avoids ...

  9. 【设计模式】行为型05责任链模式(Chain of responsibility Pattern)

    学习地址:http://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html demo采用了DEBUG级别举例子,理解起 ...

随机推荐

  1. Codeforces Round #442 (Div. 2) Danil and a Part-time Job

    http://codeforces.com/contest/877/problem/E 真的菜的不行,自己敲一个模板,到处都是问题.哎 #include <bits/stdc++.h> u ...

  2. jenkins发布java项目

    前言:这台jenkins服务器的环境是前几篇博客一步步做实验做过来,如果有想做这篇博客的实验的朋友,可以移驾去看一下前几篇博客,另外有看着博客做完的博友,可以在下方留言,证明我做的这些都是对的,有看着 ...

  3. 笔记-迎难而上之Java基础进阶4

    内部类创建多线程 //匿名内部类创建多线程 public class InnerClassThread{ public static void main(String[] args){ //回忆一下之 ...

  4. BZOJ 2085 [POI2010] Hamsters

    题面 Description Tz养了一群仓鼠,他们都有英文小写的名字,现在Tz想用一个字母序列来表示他们的名字,只要他们的名字是字母序列中的一个子串就算,出现多次可以重复计算.现在Tz想好了要出现多 ...

  5. SVG动画实践篇-字母切换

    git: https://github.com/rainnaZR/svg-animations/tree/master/src/pages/step2/letter.change 说明 这个页面实现了 ...

  6. javascript 函数初探 (六)--- 闭包初探#3

    相关定义与闭包: 实际上,每个函数都可以被认为是一个闭包.因为每个函数都在其所在域(即该函数的作用域)中维护了某种联系. 但在大多数的时候,该作用于在函数体内被执行完之后就被自行销毁了.---除非发生 ...

  7. Could not change executable permissions on the application

    I could solve it erasing an application that I had previously uploaded using the same Bundle Identif ...

  8. arcgis andriod 加载影像

    MapView mMapView;......String rasterPath = Environment.getExternalStorageDirectory().getPath() + &qu ...

  9. ES6新语法学习

    参考: 1.http://es6.ruanyifeng.com/#docs/let#let-命令 2.https://reactjs.org/tutorial/tutorial.html 3.http ...

  10. 【转载】使用事件模型 & libev学习

    参考这篇文章: http://www.ibm.com/developerworks/cn/linux/l-cn-edntwk/ 这里面使用的是 libev ,不是libevent Nodejs就是采用 ...