[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=13770720612900311&size=800_600&devtype=web&v=1.0.1&rtick=14676146422397&share_qid=108635719&sign=95e0767d82a7ea653b733a41947eaa64&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14667381119417561&size=800_600&devtype=web&v=1.0.1&rtick=14667381326962&share_qid=108635719&sign=ea4cc36f1685f2029a4fc7399492a314&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14667381119423765&size=800_600&devtype=web&v=1.0.1&rtick=14667381321470&share_qid=108635719&sign=fcd75f69ce5fdfabf68fa2f8ba1c7e59&[/img]
[color=red][b]头文件[/b][/color]
[color=red][b]实现[/b][/color]
[color=red][b]客户端[/b][/color]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14667381119417561&size=800_600&devtype=web&v=1.0.1&rtick=14667381326962&share_qid=108635719&sign=ea4cc36f1685f2029a4fc7399492a314&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14667381119423765&size=800_600&devtype=web&v=1.0.1&rtick=14667381321470&share_qid=108635719&sign=fcd75f69ce5fdfabf68fa2f8ba1c7e59&[/img]
[color=red][b]头文件[/b][/color]
//ChainOfResponsibilityPattern.h
#ifndef CHAIN_OF_RESPONSIBILITY_PATTERN_H
#define CHAIN_OF_RESPONSIBILITY_PATTERN_H
#include <Windows.h>
#include <iostream>
using namespace std;
#define SAFE_DELETE(p) if (p) { delete p; p = NULL; }
namespace ChainOfResponsibilityPattern
{
//
class HolidayRequest
{
public:
HolidayRequest(int hour);
int GetHour();
private:
int m_iHour;
};
//The holiday request handler interface
class Manager
{
public:
Manager();
virtual ~Manager();
virtual bool HandleRequest(HolidayRequest* pRequest) = 0;
};
// Project manager
class PM : public Manager
{
public:
PM(Manager *handler);
virtual ~PM();
bool HandleRequest(HolidayRequest* pRequest);
private:
Manager *m_pHandler;
};
// Department manager
class DM : public Manager
{
public:
DM(Manager *handler);
virtual ~DM();
bool HandleRequest(HolidayRequest* pRequest);
// The department manager is in?
bool IsIn();
private:
Manager *m_pHandler;
};
// Project supervisor
class PS : public Manager
{
public:
PS(Manager *handler);
virtual ~PS();
bool HandleRequest(HolidayRequest* pRequest);
private:
Manager *m_pHandler;
};
//
void ChainOfResponsibilityPattern_Test();
}
#endif
[color=red][b]实现[/b][/color]
#include "ChainOfResponsibilityPattern.h"
namespace ChainOfResponsibilityPattern
{
//
HolidayRequest::HolidayRequest(int hour)
: m_iHour(hour)
{}
int HolidayRequest::GetHour()
{
return m_iHour;
}
//
Manager::Manager(){}
Manager::~Manager(){}
//
PM::PM(Manager *handler)
: m_pHandler(handler)
{}
PM::~PM()
{}
bool PM::HandleRequest(HolidayRequest *pRequest)
{
if (pRequest->GetHour() <= 2 || m_pHandler == NULL)
{
cout<<"PM said:OK, it's Over!"<<endl;
return true;
}
else
{
cout<<"PM said:OK, transfer to next Manager."<<endl;
return m_pHandler->HandleRequest(pRequest);
}
}
// Department manager
//
DM::DM(Manager *handler)
: m_pHandler(handler)
{}
DM::~DM()
{}
bool DM::HandleRequest(HolidayRequest *pRequest)
{
cout<<"DM said:OK, it's Over!"<<endl;
return true;
}
// The department manager is in?
bool DM::IsIn()
{
return true;
}
// Project supervisor
//
PS::PS(Manager *handler)
: m_pHandler(handler)
{}
PS::~PS()
{}
bool PS::HandleRequest(HolidayRequest *pRequest)
{
DM *pDM = dynamic_cast<DM *>(m_pHandler);
if (pDM != NULL)
{
if (pDM->IsIn())
{
return pDM->HandleRequest(pRequest);
}
}
cout<<"PS said:OK, it's Over!"<<endl;
return true;
}
//
void ChainOfResponsibilityPattern_Test()
{
DM *pDM = new DM(NULL);
PS *pPS = new PS(pDM);
PM *pPM = new PM(pPS);
HolidayRequest *pHolidayRequest = new HolidayRequest(10);
pPM->HandleRequest(pHolidayRequest);
SAFE_DELETE(pHolidayRequest);
pHolidayRequest = new HolidayRequest(2);
pPM->HandleRequest(pHolidayRequest);
SAFE_DELETE(pDM);
SAFE_DELETE(pPS);
SAFE_DELETE(pPM);
SAFE_DELETE(pHolidayRequest);
}
}
[color=red][b]客户端[/b][/color]
#include "ChainOfResponsibilityPattern.h"
#include <iostream>
using namespace std;
using namespace ChainOfResponsibilityPattern;
void main()
{
ChainOfResponsibilityPattern_Test();
}
PM said:OK,transfer to next Manager.
DM said:OK,it's Over!
PM said:OK,it's Over!