cocos 2 dxリスナーモード
1475 ワード
#ifndef __LuckyEgg_V2_1__Observer__
#define __LuckyEgg_V2_1__Observer__
#include <iostream>
#include <list>
#include <map>
using namespace std;
using std::list;
class MySubject;
//
class Observer
{
public:
Observer(){}
virtual ~Observer(){}
virtual void Update(const std::string& message_type, MySubject* subject) = 0;
};
//
class MySubject
{
public:
MySubject(){}
virtual ~MySubject(){}
void Attach(const std::string& message_type, Observer *observer){ m_observers[message_type].push_back(observer);}//
void Remove(const std::string& message_type, Observer *observer){ m_observers[message_type].remove(observer);}//
void Notify(const std::string& message_type) //
{
list<Observer*>::iterator iter = m_observers[message_type].begin();
for (; iter != m_observers[message_type].end(); iter++) {
(*iter)->Update(message_type, this);
}
}
virtual void SetStatus(string s) = 0;//
virtual string GetStatus() = 0;//
private:
map<std::string, list<Observer*> >m_observers;//
protected:
string m_status;//
};
#endif /* defined(__LuckyEgg_V2_1__Observer__) */