行为模式(Behavioral Pattern)是对在不同的对象之间划分责任和算法的抽象化。行为模式不仅仅是关于类和对象的,而且是关于它们之间的相互作用的。
行为模式分为类的行为模式和对象的行为模式两种。
- 类的行为模式:类的行为模式使用继承关系在几个类之问分配行为。
- 对象的行为模式:对象的行为模式则使用对象的聚合来分配行为。
在后面将要介绍的行为模式包括以下几种:
- Chain of Resp.(责任链模式)A way of passing a request between a chain of objects
- Command(命令模式)Encapsulate a command request as an object
- Interpreter(解释器模式)A way to include language elements in a program
- Iterator(迭代子模式)Sequentially access the elements of a collection
- Mediator(中介者模式)Defines simplified communication between classes
- Memento(备忘录模式)Capture and restore an object's internal state
- Observer(观察者模式)A way of notifying change to a number of classes
- State(状态模式)Alter an object's behavior when its state changes
- Strategy(策略模式)Encapsulates an algorithm inside a class
- Template Method(模版方法模式)Defer the exact steps of an algorithm to a subclass
- Visitor(访问者模式)Defines a new operation to a class without change
一、 职责链(Chain of Responsibility)模式
责任链模式是一种对象的行为模式【GOF95】。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织链和分配责任。
从击鼓传花谈起
击鼓传花是一种热闹而又紧张的饮酒游戏。在酒宴上宾客依次坐定位置,由一人击鼓,击鼓的地方与传花的地方是分开的,以示公正。开始击鼓时,花束就开始依次传递,鼓声一落,如果花束在某人手中,则该人就得饮酒。
击鼓传花便是责任链模式的应用。责任链可能是一条直线、一个环链或者一个树结构的一部分。
二、 责任链模式的结构
责任链模式涉及到的角色如下所示:
抽象处理者(Handler)角色:定义出一个处理请求的接口。如果需要,接口可以定义出一个方法,以设定和返回对下家的引用。这个角色通常由一个抽象类或接口实现。
具体处理者(ConcreteHandler)角色:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。
#ifndef CHAIN_OF_RESPONSIBILITY_H_
#define CHAIN_OF_RESPONSIBILITY_H_
class Handler
{
public:
void SetSuccessor(Handler* apSuccessor){ m_pSuccessor = apSuccessor;}
virtual void HandleRequest(int aiReq) = 0;
protected:
Handler* m_pSuccessor;
};
class ConcreateHandler1 : public Handler
{
public:
virtual void HandleRequest(int aiReq);
};
class ConcreateHandler2 : public Handler
{
public:
virtual void HandleRequest(int aiReq);
};
class ConcreateHandler3 : public Handler
{
public:
virtual void HandleRequest(int aiReq);
};
#endif
#include "stdafx.h"
#include "ChainOfResponsibility.h"
#include <iostream>
using namespace std;
void ConcreateHandler1::HandleRequest(int aiReq)
{
if (aiReq >= 0 && aiReq < 10)
{
cout<<"ConcreateHandler1::HandleRequest "<<aiReq<<endl;
}
else
{
if (m_pSuccessor)
{
m_pSuccessor->HandleRequest(aiReq);
}
}
}
void ConcreateHandler2::HandleRequest(int aiReq)
{
if (aiReq >= 10 && aiReq < 20)
{
cout<<"ConcreateHandler2::HandleRequest "<<aiReq<<endl;
}
else
{
if (m_pSuccessor)
{
m_pSuccessor->HandleRequest(aiReq);
}
}
}
void ConcreateHandler3::HandleRequest(int aiReq)
{
if (aiReq >= 20 && aiReq < 30)
{
cout<<"ConcreateHandler3::HandleRequest "<<aiReq<<endl;
}
else
{
if (m_pSuccessor)
{
m_pSuccessor->HandleRequest(aiReq);
}
}
}
#include "stdafx.h"
#include "ChainOfResponsibility.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Handler* lpH1 = new ConcreateHandler1;
Handler* lpH2 = new ConcreateHandler2;
Handler* lpH3 = new ConcreateHandler3;
lpH1->SetSuccessor(lpH2);
lpH2->SetSuccessor(lpH3);
int a[9] = { 1, 2, 3 , 11, 12, 13, 21, 22, 23 };
for (int li = 0 ; li< 9 ; li++)
{
lpH1->HandleRequest(a[li]);
}
delete lpH3;
delete lpH2;
delete lpH1;
system("pause");
return 0;
}