在软件构建构成中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个接收者,如果显示指定,将必不可少地带来请求发送者与接收者的紧密耦合。COR(Chain of Reposibility)设计模式可以使请求的发送者不需要指定具体的接收者,让请求的接收者自己在运行时决定来处理请求,从而使两者解耦。
“Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.” - GoF
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象对应它为止。
其UML类图如下:
C++示例代码如下:
说明:
- 经理级别可以决定是否采购500RMB以下的物品;
- 总监级别可以决定是否采购5000RMB以下的物品;
- 总裁级别可以决定是否采购任何物品。
// COR.h
#include <string>
#include <iostream>
#include <memory>
using namespace std;
// 消息类
class CORMessage
{
private:
double price;
public:
CORMessage(double price) : price(price)
{
}
double get_price()
{
return price;
}
};
// 抽象类Handler
class Handler
{
protected:
//Handler *next_handler;
auto_ptr<Handler> next_handler;
public:
//Handler(Handler *next_handler) : next_handler(next_handler)
Handler(auto_ptr<Handler> next_handler) : next_handler(next_handler)
{
}
virtual void handleMessage(CORMessage cor_message) = 0;
virtual bool canHandleMessage(CORMessage cor_message) = 0;
public:
virtual ~Handler()
{
cout << "in the destructor of Handler..." << endl;
}
};
// 具体处理类
class ConcreteManagerHandler : public Handler
{
public:
//ConcreteManagerHandler(Handler *next_handler) : Handler(next_handler)
ConcreteManagerHandler(auto_ptr<Handler> next_handler) : Handler(next_handler)
{
}
~ConcreteManagerHandler()
{
cout << "in the destructor of ConcreteManagerHandler..." << endl;
}
public:
void handleMessage(CORMessage cor_message)
{
if(canHandleMessage(cor_message))
{
cout << "MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteManagerHandler" << endl;
}
else
{
next_handler->handleMessage(cor_message);
}
}
bool canHandleMessage(CORMessage cor_message)
{
if(cor_message.get_price() < 500.0)
return true;
else
return false;
}
};
// 具体处理类
class ConcreteDirectorHandler : public Handler
{
public:
//ConcreteDirectorHandler(Handler *next_handler) : Handler(next_handler)
ConcreteDirectorHandler(auto_ptr<Handler> next_handler) : Handler(next_handler)
{
}
~ConcreteDirectorHandler()
{
cout << "in the destructor of ConcreteDirectorHandler..." << endl;
}
public:
void handleMessage(CORMessage cor_message)
{
if(canHandleMessage(cor_message))
{
cout << "MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteDirectorHandler" << endl;
}
else
{
next_handler->handleMessage(cor_message);
}
}
bool canHandleMessage(CORMessage cor_message)
{
if(cor_message.get_price() < 5000.0)
return true;
else
return false;
}
};
// 具体处理类
class ConcreteOfficerHandler : public Handler
{
public:
//ConcreteOfficerHandler(Handler *next_handler) : Handler(next_handler)
ConcreteOfficerHandler(auto_ptr<Handler> next_handler) : Handler(next_handler)
{
}
~ConcreteOfficerHandler()
{
cout << "in the destructor of ConcreteOfficerHandler..." << endl;
}
public:
void handleMessage(CORMessage cor_message)
{
if(canHandleMessage(cor_message))
{
cout << "MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteOfficerHandler" << endl;
}
else
{
next_handler->handleMessage(cor_message);
}
}
bool canHandleMessage(CORMessage cor_message)
{
return true; // 总裁级别可以决定购买任何价格的物品
}
};
// COR.cpp
#include "COR.h"
int main(int argc, char **argv)
{
//Handler *officer_handler = new ConcreteOfficerHandler(0);
//Handler *director_handler = new ConcreteDirectorHandler(officer_handler);
//Handler *manager_handler = new ConcreteManagerHandler(director_handler);
auto_ptr<Handler> officer_handler(new ConcreteOfficerHandler((auto_ptr<Handler>)0));
auto_ptr<Handler> director_handler(new ConcreteDirectorHandler(officer_handler));
auto_ptr<Handler> manager_handler(new ConcreteManagerHandler(director_handler));
manager_handler->handleMessage(CORMessage(350.0));
manager_handler->handleMessage(CORMessage(600.0));
manager_handler->handleMessage(CORMessage(10000.11));
//delete officer_handler;
//delete director_handler;
//delete manager_handler;
return 0;
}
输出结果:
MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteManagerHandler
MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteDirectorHandler
MESSAGE IS HANDLED SUCCESSFULLY! -- ConcreteOfficerHandler
in the destructor of ConcreteManagerHandler...
in the destructor of Handler...
in the destructor of ConcreteDirectorHandler...
in the destructor of Handler...
in the destructor of ConcreteOfficerHandler...
in the destructor of Handler...
从输出结果可见,
购买350.0RMB的物品被ConcreteManagerHandler处理了;
购买600.0RMB的物品被ConcreteDirectorHandler处理了;
购买10000.11RMB的物品被ConcreteOfficerHandler处理了。
注:如果不使用auto_ptr,则可以用被注释的代码部分代替。