码迷,mamicode.com
首页 > 编程语言 > 详细

C++设计模式之职责链模式

时间:2018-07-12 20:19:07      阅读:14      评论:0      收藏:0      [点我收藏+]

标签:char*   style   ret   定义   string   mon   out   eal   ++   

代码实现:

// chainResbonsibility.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


class Manager
{
protected:
    Manager *m_manager;
    string m_name;
public:
    Manager(Manager* manager,string name):m_manager(manager),m_name(name){}
    virtual void DealWithRequest(string name, int num) {}
};

class CommonManager : public Manager
{
public:
    CommonManager(Manager* manager,string name):Manager(manager,name){}
    void DealWithRequest(string name,int num)
    {
        if(num < 500)
        {
            cout << "经理" <<m_name << "批准" << name << "加薪" << num << "" <<endl;
        }
        else
        {
            cout << "经理" << m_name << "无法处理,交由总监处理" <<endl;
            m_manager->DealWithRequest(name,num);
        }
    }
};

class Majordomo:public Manager
{
public:
    Majordomo(Manager *manager,string name):Manager(manager,name){}
    virtual void DealWithRequest(string name,int num)
    {
        if(num < 1000)
        {
            cout << "总监" <<m_name << "批准" << name << "加薪" << num << "" <<endl;
        }
        else
        {
            cout << "总监" << m_name << "无法处理,交由总经理处理" <<endl;
            m_manager->DealWithRequest(name,num);
        }
    }
};

class GeneralManager : public Manager
{
public:
    GeneralManager(Manager *manager,string name):Manager(manager,name){}
    void DealWithRequest(string name,int num)
    {
        cout << "总经理" <<m_name << "批准" << name << "加薪" << num << "" <<endl;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    Manager *general = new GeneralManager(NULL,"a");
    Manager *majordomo = new Majordomo(general,"b");
    Manager *common = new CommonManager(majordomo,"c");
    common->DealWithRequest("d",400);
    common->DealWithRequest("d",700);
    common->DealWithRequest("d",1200);
    system("pause");
    return 0;
}

 

C++设计模式之职责链模式

标签:char*   style   ret   定义   string   mon   out   eal   ++   

原文地址:https://www.cnblogs.com/wxmwanggood/p/9300960.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!