C++观察者模式例子

观察者模式定义了对象间一对多的依赖关系,一个目标可以有任意数目的与之相依赖的观察者,一旦观察目标的状态发生改变,所有的观察者都将得到通知。
observer.h

#include <string>
#include <stdio.h>
class CSubject;
class CObserver
{
public:
    CObserver(){}
    virtual ~CObserver(){}
    virtual void Update(CSubject* apoSubject) = 0;
};

class CConcreteObserverA : public CObserver
{
public:
    CConcreteObserverA(CSubject* apoSubject);
    virtual ~CConcreteObserverA(){}
    void Update(CSubject* apoSubject);
private:
    CSubject* mpoSubject;
    std::string moState;
};

class CConcreteObserverB : public CObserver
{
public:
    CConcreteObserverB(CSubject* apoSubject);
    virtual ~CConcreteObserverB(){}
    void Update(CSubject* apoSubject);
private:
    CSubject* mpoSubject;
    std::string moState;
};

observer.cpp

#include "observer.h"
#include "subject.h"
CConcreteObserverA::CConcreteObserverA(CSubject* apoSubject)
{
    mpoSubject = apoSubject;
    apoSubject->Attach(this);
}

void CConcreteObserverA::Update(CSubject* apoSubject)
{
    moState = apoSubject->GetState();
    printf("state %s\n", moState.c_str());
}

CConcreteObserverB::CConcreteObserverB(CSubject* apoSubject)
{
    mpoSubject = apoSubject;
    apoSubject->Attach(this);
}

void CConcreteObserverB::Update(CSubject* apoSubject)
{
    moState = apoSubject->GetState();
    printf("state %s\n", moState.c_str());
}

subject.h

#include <string>
#include <list>
class CObserver;
class CSubject
{
public:
    CSubject(){}
    virtual ~CSubject(){}
    virtual void SetState(std::string aoState) = 0;
    virtual std::string GetState() = 0;
    void Attach(CObserver* apoObserver);
    void Dettach(CObserver* apoObserver);
    void Notify();
private:
    std::list<CObserver*> moObserverList;
};

class CConcreteSubjectA : public CSubject
{
public:
    CConcreteSubjectA(){}
    virtual ~CConcreteSubjectA(){}
    void SetState(std::string aoState);
    std::string GetState();
private:
    std::string moState;
};

subject.cpp

#include "subject.h"
#include "observer.h"

void CSubject::Attach(CObserver* apoObserver)
{
    moObserverList.push_front(apoObserver);
}

void CSubject::Dettach(CObserver* apoObserver)
{
    moObserverList.remove(apoObserver);
}

void CSubject::Notify()
{
    std::list<CObserver*>::iterator loIter = moObserverList.begin();
    for (; loIter != moObserverList.end(); ++loIter)
    {
        (*loIter)->Update(this);
    }
}

void CConcreteSubjectA::SetState(std::string aoState)
{
    moState = aoState;
}

std::string CConcreteSubjectA::GetState()
{
    return moState;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值