Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32888
  • 博文数量: 9
  • 博客积分: 177
  • 博客等级: 入伍新兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-11 19:12
文章分类

全部博文(9)

文章存档

2011年(9)

我的朋友
最近访客

分类: C/C++

2011-10-05 23:06:20

chain of responsibility

责任链模式

案例:三从四德的三从是指出嫁前听父亲的。出嫁了听丈夫的,丈夫不幸去世了,听儿子的。

这个怎么实现呢。

Iwomen接口

 

#ifndef _IWOMEN_H_
#define _IWOMEN_H_
#include
using namespace std;
class iwomen
{
        public :
                virtual int gettype(){};
                virtual string & getrequest(){};
                virtual ~iwomen(){};
};
#endif

women

#ifndef _WOMEN_H_
#define _WOMEN_H
#include
#include "iwomen.h"
#include
using namespace std;
class women :public iwomen
{
        public:
                women(int _type,string & _request):type(_type),request(_request){};
                int gettype(){return type;};
                string &getrequest(){return request;};
        private:
                int type;
                string request;

};
#endif

处理请求的基类


#ifndef _IHANDLE_H_
#define _IHANDLE_H_
#include "iwomen.h"
class ihandle
{
        public:
                virtual void handlemessage(iwomen *women){};
                virtual ~ihandle(){};
};
#endif

父亲类

#ifndef _FATHER_H_
#define _FATHER_H_
#include
#include "ihandle.h"
#include "iwomen.h"
class father : public ihandle
{
        public:
                void handlemessage(iwomen *women)
                {
                        std::cout<<"daughter`s request is " + women->getrequest()<                        std::cout<<"father yes"<                };
};

#endif

丈夫类

#ifndef _HUSBAND_H_
#define _HUSBAND_H_
#include
#include "ihandle.h"
#include "iwomen.h"
class husband : public ihandle
{
        public:
                void handlemessage(iwomen *women)
                {
                        std::cout<<"wife`s request is "+women->getrequest()<                        std::cout<<"husband yes"<                };
};

#endif

儿子类

#ifndef _SON_H_
#define _SON_H_
#include "ihandle.h"
#include
#include "iwomen.h"
class son : public ihandle
{
        public:
                void handlemessage(iwomen *women)
                {
                        std::cout<<"mother`s request is"+women->getrequest()<                        std::cout<<"son yes"<                };
};

#endif
~        

Main定义五个women,每一个women采用随机的方式获得他的类型(出嫁否?丈夫去世否,0代表未出嫁,1代表出嫁,2代表丈夫去世)

#include "father.h"
#include "women.h"
#include "iwomen.h"
#include "son.h"
#include "husband.h"
#include
#include
#include
#include
int main()
{
        std::string str("shopping");
        srand(time(0));
        vector vec;
        for(int i = 0;i<5;i++)
        {
                int z = rand()%3;
                //std::cout<                women w(z,str);
                vec.push_back(w);
        }
        father f;
        son s;
        husband h;
        vector::iterator iter = vec.begin();
        for(;iter != vec.end(); iter++)
        {
                if(iter->gettype() == 0)
                        f.handlemessage(&(*iter));
                else if(iter->gettype() == 1)
                        h.handlemessage(&(*iter));
                else s.handlemessage(&(*iter));
        }
        return 0;
}

 

 

 

 

 
阅读(1160) | 评论(0) | 转发(0) |
0

上一篇:设计模式之Command模式

下一篇:没有了

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