C++ 观察者模式 (Oberver Pattern)

由于是个人练习用的,而且时间仓促,没有想好后再来写,尽请见谅!

//UtilTool.h

#pragma once
#include
#include
#include

using namespace std;

//Event.h

#pragma once
#include "UtilTool.h"
enum EVENT_STATE
{
    EAWAKE = 0,
    ESTART = 1,
    ETICK = 2,
    EEND = 3,
    ELEAVE = 4,
    EDESTROY = 5,
    ENORMAL = 6,
    ECLICK =7,
    EOVERLAP =8
};
class Event
{
public:
    Event() :mCurrentState(EVENT_STATE::EAWAKE) {}
    virtual void Awake() { cout << "This is basic awake!"<     virtual void Start() { cout << "This is basic start!"<     virtual void Tick() { cout << "This is basic tick!"<     virtual void End() { cout << "This is basic end!"<     virtual void Overlap() { cout << "This is basic overlap!"<     virtual void Destroy() { cout << "This is basic destroy!"<     EVENT_STATE mCurrentState;
};

//Object.h

#pragma once
#include "UtilTool.h"
#include "Event.h"
class Object:public Event
{
public:
    string mName;
    string mDesc;
    long long mId;
    Object(string name, string desc,long long id) :mName(name), mDesc(desc),mId(id) {}
    void ShowBaseSomeThing()
    {
        cout << "<--Id : " << mId << ";Name : " << mName << ";Desc :" << mDesc << "-->" << endl;
    }
};

//TSubject.h

#pragma once
template
class TSubject
{
public :
    virtual void Attach(T*) = 0;
    virtual void Detach(T*) = 0;
    virtual void Notify() = 0;
};

//TConcreteSubject.h

#pragma once
#include "UtilTool.h"
#include "TSubject.h"
#include "TObserver.h"
#include "Event.h"
template
class TConcreteSubjectUI :public TSubject
{
public:
    virtual void Attach(T *);
    virtual void Detach(T *);
    virtual void Notify();
private :
    static list *mObserverList;
};
template
list* TConcreteSubjectUI::mObserverList = new list();
template
void TConcreteSubjectUI::Attach(T* observer)
{
    mObserverList->push_back(observer);
}
template
void TConcreteSubjectUI::Detach(T* observer)
{
    mObserverList->remove(observer);
}
template
void TConcreteSubjectUI::Notify()
{
    list::iterator it ;
    for(it = mObserverList->begin(); it != mObserverList->end();++it)
    {
        TConcreteObserver *ob = (TConcreteObserver*)(*it);
        Object* obj = ob->GetObject();
        ob->UpdateState(obj);
    }

}

//TObserver.h

#pragma once
template
class TObserver
{
public :
    virtual void UpdateState(T*)=0;
};

//TConcreteObserver.h

#pragma once
#include "UtilTool.h"
#include "TObserver.h"
#include "Object.h"
#include "Event.h"
template
class TConcreteObserver:public TObserver
{
public :
    TConcreteObserver(Object *obj) :mObject(obj) {}
    virtual void UpdateState(T*);
    Object* GetObject();
private:
    Object* mObject;
};
template
Object* TConcreteObserver::GetObject()
{
    return mObject;
}
template
void TConcreteObserver::UpdateState(T* object)
{
    Event *event =  object;
    switch (event->mCurrentState)
    {
    case EVENT_STATE::EAWAKE:
        event->Awake();
        break;
    case EVENT_STATE::ESTART:
        event->Start();
        break;
    case EVENT_STATE::ETICK:
        event->Tick();
        break;
    case EVENT_STATE::EEND:
        event->End();
        break;
    case EVENT_STATE::EOVERLAP:
        event->Overlap();
        break;
    case EVENT_STATE::EDESTROY:
        event->Destroy();
        break;
    default:
        break;
    }
    Object* ob = object;
    ob->ShowBaseSomeThing();
}

//test.cpp

#include "UtilTool.h"
#include "TObserver.h"
#include "TSubject.h"
#include "TConcreteObserver.h"
#include "TConcreteSubject.h"
int main()
{
    TConcreteSubjectUI> *tb = new TConcreteSubjectUI>();
    Object *obj01 = new Object("ZhangSan","BigHead",1);
    TConcreteObserver *tr = new TConcreteObserver(obj01);
    Object *obj02 = new Object("LiSi", "SmallHead", 2);
    TConcreteObserver *tr2 = new TConcreteObserver(obj02);
    tb->Attach(tr2);
    tb->Attach(tr);
    tb->Notify();
    obj01->mCurrentState = EVENT_STATE::EOVERLAP;
    obj02->mCurrentState = EVENT_STATE::EOVERLAP;
    tb->Notify();
    tb->Detach(tr2);
    tb->Notify();
    system("pause");
    return 0;
}

//目录结构

C++ 观察者模式 (Oberver Pattern)_第1张图片

//执行结果

C++ 观察者模式 (Oberver Pattern)_第2张图片


你可能感兴趣的:(C++,Patterns,Beginning)