posts - 11,comments - 13,trackbacks - 0
Observer分为推模式和拉模式;区别在于observer是被动还是主动;
推模式中,分发者不管observer是否需要信息,都统一的下发相同的更新给各个observer;
拉模式中,由observer主动发起,仅仅向分发者请求自己需要的信息。
如果分发者中的数据结构体发生的变化,那么每个observer也会要进行相应的改变;但如果
用拉模式,observer对象的处理可以不需要变化。

以下为observer的推模式代码:
 1#ifndef __OBSERVER_H__
 2#define __OBSERVER_H__
 3#include "Subject.h"
 4
 5class CObserver
 6{
 7protected:
 8    CObserver()
 9    {
10
11    }

12public:
13    virtual ~CObserver()
14    {
15
16    }

17
18    virtual void Update(CSubject *= 0;
19
20    virtual void PrintMsg(INFO) = 0;
21
22}
;
23
24class CExtendObserver:public CObserver
25{
26public:
27    CExtendObserver(CSubject *data)
28    {
29        _sub = data;
30        _sub->AddObserver(this);
31    }

32
33    ~CExtendObserver()
34    {
35        _sub->DelObserver(this);
36        if (_sub != 0
37        
38            delete _sub; 
39            _sub = NULL;
40        }

41    }

42    void Update(CSubject *);
43    void PrintMsg(INFO );
44private:
45    CSubject * _sub;
46}
;
47
48class CExtendObserver1:public CObserver
49{
50public:
51    CExtendObserver1(CSubject * data)
52    {
53        _sub = data;
54        _sub->AddObserver(this);
55    }

56    ~CExtendObserver1()
57    {
58        _sub->DelObserver(this);
59        if (_sub != 0
60        
61            delete _sub; 
62            _sub = NULL;
63        }

64    }

65    void Update(CSubject *);
66    void PrintMsg(INFO);
67private:
68    CSubject * _sub;
69}
;
70
71
72
73#endif
 1#ifndef __SUBJECT_H__
 2#define __SUBJECT_H__
 3
 4#include <iostream>
 5#include <vector>
 6
 7class CObserver;
 8
 9typedef struct _INFO_
10{
11    std::string m_desc;
12    std::string m_temp;
13    _INFO_()
14    {
15        m_desc = "Description!";
16        m_temp = "Temperature";
17    }

18}
INFO;
19
20class CSubject
21{
22protected:
23    CSubject()
24    {
25        _obs = NULL;
26        _obs = new std::vector<CObserver *>;
27    }

28
29public:
30    virtual ~CSubject()
31    {
32        if(!(*_obs).empty())
33        {
34            (*_obs).clear();
35        }

36        delete(_obs);
37        _obs = NULL;
38    }

39    virtual void AddObserver(CObserver * );
40    virtual void DelObserver(CObserver *);
41    virtual void Update( );
42    virtual void Set_info(std::string ,std::string= 0;
43    virtual INFO Get_info() = 0;
44public:
45    std::vector<CObserver *> *_obs;
46}
;
47
48class CExtendSubject:public CSubject
49{
50public:
51    CExtendSubject()
52    {
53
54    }

55    ~CExtendSubject()
56    {
57
58    }

59    void Set_info(std::string ,std::string);
60    INFO Get_info();
61public:
62    INFO m_info;
63}
;
64#endif
 1#include "observer.h"
 2
 3void CExtendObserver1::Update(CSubject * data)
 4{
 5    INFO info = data->Get_info();
 6    PrintMsg(info);
 7}

 8
 9void CExtendObserver1::PrintMsg(INFO info)
10{
11    std::cout<<"**********************"<<std::endl;
12    std::cout<<"CExtendObserver1:"<<info.m_desc.c_str()<<","<<info.m_temp.c_str()<<std::endl;
13    std::cout<<"**********************"<<std::endl;
14}

15
16
17void CExtendObserver::Update(CSubject * data)
18{
19    INFO info = data->Get_info();
20    PrintMsg(info);
21}

22
23void CExtendObserver::PrintMsg(INFO info)
24{
25    std::cout<<"**********************"<<std::endl;
26    std::cout<<"CExtendObserver:"<<info.m_desc.c_str()<<","<<info.m_temp.c_str()<<std::endl;
27    std::cout<<"**********************"<<std::endl;
28}

29
30
31
32int main(int argc,char * argv[])
33{
34    CSubject * sub = new CExtendSubject();
35    CObserver* ob1 = new CExtendObserver(sub);
36    CObserver * ob2 = new CExtendObserver1(sub);
37
38    sub->Update();
39
40    std::cout<<"#######################"<<std::endl;
41    std::cout<<"#######################"<<std::endl;
42
43    sub->Set_info("Change Desc!","Change Temperature!");
44
45    sub->Update();
46}
 1#include "Subject.h"
 2#include <algorithm>
 3#include "observer.h"
 4void CSubject::AddObserver(CObserver * data)
 5{
 6    (*_obs).push_back(data);
 7}

 8
 9void CSubject::DelObserver(CObserver * data)
10{
11    if(!data)
12    {
13        (*_obs).erase(remove((*_obs).begin(),(*_obs).end(),data));
14    }

15}

16
17void CSubject::Update()
18{
19    std::vector<CObserver*>::iterator iter = (*_obs).begin();
20    for(;iter != (*_obs).end();++iter)
21    {
22        CObserver* tmp = (*iter);
23        tmp->Update(this);
24    }

25}

26
27void CExtendSubject::Set_info(std::string desc,std::string temp)
28{
29    if(!m_info.m_desc.empty())
30    {
31        m_info.m_desc.clear();
32    }

33
34    if(!m_info.m_temp.empty())
35    {
36        m_info.m_temp.clear();
37    }

38
39    m_info.m_temp = temp;
40    m_info.m_desc = desc;
41}

42
43
44INFO CExtendSubject::Get_info()
45{
46    return m_info;
47}
posted on 2009-06-18 18:59 Super- 阅读(1315) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理