Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54167
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 18:59
文章分类
文章存档

2015年(5)

2014年(1)

2013年(9)

我的朋友

分类: C/C++

2013-07-24 19:07:05

突然想写个程序玩
有些东西要开始复习了,今天看到职责连模式,顺带有个例子,觉得敲着玩也好:图片
代码如下:
taos@ubuntu:~/code/cpp/cxymsbd$ cat 15.1.4.cpp
#include
#include
#include
#include

using namespace std;

class Manager{
        protected:
                Manager * m_manager;
                string m_name;
        public:
                Manager(Manager * direct_boss, string boss_name){
                        m_manager = direct_boss;
                        m_name = boss_name;
                        srand(unsigned(time(NULL)));
                }
                virtual void dealWithRequest(string employee_name, int money) = 0;
};

class BigBoss : public Manager{
        public:
        BigBoss(Manager * direct_boss, string boss_name) : Manager(direct_boss, boss_name){}

        virtual void dealWithRequest(string employee_name, int money){
                cout << employee_name << " want increase " << money << endl;
                if (rand() % 1000 >= 800){
                        cout << "BigBoss allowed"<< employee_name <<" increasing " << money << ", haha!!!" << endl;
                }else {
                        cout << "BigBoss : NOWAY!!!" << endl;
                }
        }
};


class majorManager : public Manager{
        public:
        majorManager(Manager * direct_boss, string boss_name) : Manager(direct_boss, boss_name){}

        virtual void dealWithRequest(string employee_name, int money){
                if(money < 2000){
                        cout << employee_name << " want increase " << money << endl;
                        if (rand() % 1000 >= 500){
                                cout << "majorMamager allowed "<< employee_name <<" increasing " << money << ", haha!!!" << endl;
                        }else {
                                cout << "majorManager : NOWAY!!!" << endl;
                        }
                }else{
                        m_manager->dealWithRequest(employee_name, money);
                }
        }
};

class commonManager : public Manager{
        public:
        commonManager(Manager * direct_boss, string boss_name): Manager(direct_boss, boss_name){}

        virtual void dealWithRequest(string employee_name, int money){
                if(money < 800){
                        cout << employee_name << " want increase " << money << endl;
                        if (rand() % 1000 >= 300){
                                cout << "commonManager allowed "<< employee_name <<" increasing " << money << ", haha!!!" << endl;
                        }else {
                                cout << "commonManager : NOWAY!!!" << endl;
                        }

                }else{
                        m_manager->dealWithRequest(employee_name, money);
                }
        }
};

int main(){
        BigBoss BigBoom(NULL,"taoge");
        majorManager senior(&BigBoom,"major");
        commonManager common(&senior,"common_manager_oo");

        common.dealWithRequest("a",400);
        common.dealWithRequest("b",500);
        common.dealWithRequest("c",800);
        common.dealWithRequest("d",1000);
        common.dealWithRequest("e",5000);

        return 0;
}

结果如下:
taos@ubuntu:~/code/cpp/cxymsbd$ ./a.out
a want increase 400
commonManager allowed a increasing 400, haha
b want increase 500
commonManager allowed b increasing 500, haha
c want increase 800
majorManager : NOWAY!!!
d want increase 1000
majorMamager allowed d increasing 1000, haha
e want increase 5000
BigBoss : NOWAY!!! 

敲一敲也有意思 O(∩_∩)O哈哈哈~ 
 
阅读(965) | 评论(2) | 转发(0) |
0

上一篇:没有了

下一篇:优秀开源组合:助你快速研发

给主人留下些什么吧!~~

4大爷2013-07-25 09:35:27

  期待更多的博文