设计模式 职责链模式

467阅读 0评论2011-07-04 jhluroom
分类:项目管理

1、request.h

#ifndef REQUEST_H
#define REQUEST_H

#include <iostream>
#include <string>
using namespace std;

class Request //请求类定义

{
public:
    int number;//当类型为0时,此为请假的天数;当类型为1时,此为加薪的数目;当类型为2时,....

    int requestType;//请求类型,0为请假;1为加薪;2...

    string requestContent;//请求的具体内容

};
#endif // REQUEST_H



2、manager.h

#ifndef MANAGER_H
#define MANAGER_H

#include "request.h"

#include <iostream>
#include <string>
using namespace std;

class Manager //处理请求的基类,由具体的处理类加以实现

{
protected:
    string name;
    Manager* superior;

public:
    Manager(const string& s) : name(s), superior(0) {}

    void SetSuperior(Manager* m)
    {
        superior = m;
    }

    virtual void RequestApplications(Request* request) = 0;
};
#endif // MANAGER_H



3、commonmanager.h

#ifndef COMMONMANAGER_H
#define COMMONMANAGER_H

#include "manager.h"

#include <iostream>
#include <string>
using namespace std;

class CommonManager : public Manager //一般经理类

{
public:
    CommonManager(const string& s) : Manager(s) {}

    virtual void RequestApplications(Request* request)
    {
        if (request->requestType == 0 && request->number <= 2)
        {
            cout << " be approved by "<< name << ", " << "Type:"<<request->requestType <<", Content:"<<request->requestContent << ", Number:" << request->number << endl;
        }
        else if (superior != 0)
        {
            superior->RequestApplications(request);
        }
        else
        {
            cout << "Can not be processed!" << endl;
        }
    }
};
#endif // COMMONMANAGER_H



4、majormanager.h

#ifndef MAJORMANAGER_H
#define MAJORMANAGER_H

#include "manager.h"

#include <iostream>
#include <string>
using namespace std;


class MajorManager : public Manager //小组经理类

{
public:
    MajorManager(const string& s) : Manager(s) {}

    virtual void RequestApplications(Request* request)
    {
        if (request->requestType == 0 && request->number <= 7)
        {
            cout << " be approved by " << name << ", " << "Type:"<<request->requestType <<", Content:"<<request->requestContent << ", Number:" << request->number << endl;
        }
        else if (superior != 0)
        {
            superior->RequestApplications(request);
        }
        else
        {
            cout << "Can not be processed!" << endl;
        }
    }
};
#endif // MAJORMANAGER_H



5、generalmanager.h

#ifndef GENERALMANAGER_H
#define GENERALMANAGER_H

#include "manager.h"

#include <iostream>
#include <string>
using namespace std;

class GeneralManager : public Manager //总经理类

{
public:
    GeneralManager(const string& s) : Manager(s) {}

    virtual void RequestApplications(Request* request)
    {
        if (request->requestType == 0 && request->number <= 14)
        {
            cout << " be approved by " << name << ", " << "Type:"<<request->requestType <<", Content:"<<request->requestContent << ", Number:" << request->number << endl;
        }
        else if (request->requestType == 1 && request->number <= 1000)
        {
            cout << " be approved by " << name << ", " << "Type:"<<request->requestType <<", Content:"<<request->requestContent << ", Number:" << request->number << endl;
        }
        else if (superior != 0)
        {
            superior->RequestApplications(request);
        }
        else
        {
            cout << "Can not be processed!" << endl;
        }
    }
};
#endif // GENERALMANAGER_H



6、main.cpp

/*
开发IDE:qt creater
开发环境:QT C++
参考网站:神秘果:

定义:
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。
将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

理解:
1.职责链(Chain of Responsibility)模式也许被看做一个使用策略对象的“递归的动态一般化".此时提出一个调用,
  在一个链序列中的每个策略都试图满足这个调用。这个过程直到有一个策略成功满足该调用或者到达序列的末尾才结束。
  在递归方法中,有个函数反复调用其自身至达到某个终止条件。在职责链中,一个函数调用自身,(通过遍历策略链)调用函数的一个
  不同实现,如此反复直至达到某个终止条件。这个终止条件或者是已达到策略链的底部(这样就会返回一个默认对象;
  不管能否提供这个默认结果,必须有个方法能够决定该职责链搜索是成功还是失败)或者是成功找到一个策略。
2.除了调用一个函数来满足某个请求以外,链中的多个函数都有此机会满足某个请求,因此它有点专家系统的意味。

要点:
1.职责链模式-尝试采用一系列策略模式:
2.在职责链中,一个函数调用自身,调用函数的一个不同实现,如此反复直至达到某个终止条件。
3.这个终止条件或者是已到达策略链的底部或者是成功找到一个策略。
4.本质是尝试多个解决方法直到找到一个起作用的方法。

应用:
如:员工请假和加薪请求,经理按职位组成一条链,从底到高依次为:一般经理,小组经理,总经理
当有请求时,依次经过一般经理->小组经理->总经理,在这个过种中,只要有一个经理有权处理这个请求后,这个请求即将结束。

以上文字说明,从网上整理而来,有可能部分与其他同仁相同,请谅解,希望我们能够共同交流,谢谢!
*/


#include <QtCore/QCoreApplication>

#include "request.h"
#include "commonmanager.h"
#include "majormanager.h"
#include "generalmanager.h"

int main(int argc, char *argv[])
{
    cout << "=== jhluroom start ========" << endl;

    CommonManager* managerWang = new CommonManager("Manager Wang");
    MajorManager* managerLu = new MajorManager("Manager Lu");
    GeneralManager* managerLiang= new GeneralManager("Manager Liang");

    managerWang->SetSuperior(managerLu);
    managerLu->SetSuperior(managerLiang);

    cout << "request1:";
    Request* request1 = new Request;
    request1->requestType = 0;
    request1->requestContent = "jhluroom ask for leave";
    request1->number = 1;
    managerWang->RequestApplications(request1);

    cout << "request2:";
    Request* request2 = new Request;
    request2->requestType = 0;
    request2->requestContent = "jhluroom ask for leave";
    request2->number = 4;
    managerWang->RequestApplications(request2);

    cout << "request3:";
    Request* request3 = new Request;
    request3->requestType = 0;
    request3->requestContent = "jhluroom ask for leave";
    request3->number = 10;
    managerWang->RequestApplications(request3);

    cout << "request4:";
    Request* request4 = new Request;
    request4->requestType = 1;
    request4->requestContent = "jhluroom ask for Pay";
    request4->number = 500;
    managerWang->RequestApplications(request4);

    cout << "request5:";
    Request* request5 = new Request;
    request5->requestType = 2;
    request5->requestContent = "jhluroom ask for Pay";
    request5->number = 1000;
    managerWang->RequestApplications(request5);

    cout << "=== jhluroom finish _^_ ===" << endl;
}



运行结果:

=== jhluroom start ========

request1: be approved by Manager Wang, Type:0, Content:jhluroom ask for leave, Number:1

request2: be approved by Manager Lu, Type:0, Content:jhluroom ask for leave, Number:4

request3: be approved by Manager Liang, Type:0, Content:jhluroom ask for leave, Number:10

request4: be approved by Manager Liang, Type:1, Content:jhluroom ask for Pay, Number:500

request5:Can not be processed!

=== jhluroom finish _^_ ===

上一篇:设计模式 生成器模式
下一篇:设计模式 观察者模式