#ifndef SUBJECT_H
#define SUBJECT_H
#include<list>
#include "observer.h"
using namespace std;
class Observer;
class Blog
{
private:
list<Observer *> m_observers; //??????б?
protected:
string m_status; //??
public:
Blog() {}
virtual ~Blog() {}
void Attach(Observer *observer);   //???????
void Remove(Observer *observer);    //????????
void Notify();  //???????
virtual void SetStatus(string s);   //??????
virtual string GetStatus(); //?????
};
class BlogCSDN : public Blog
{
private:
string m_name; //????????
public:
BlogCSDN(string name) : m_name(name) {}
~BlogCSDN() {}
void SetStatus(string s);
string GetStatus();
};
#endif
subject.cpp
#include "subject.h"
void Blog::Attach(Observer *observer)
{
m_observers.push_back(observer);
}
void Blog::Remove(Observer *observer)
{
m_observers.remove(observer);
}
void Blog::Notify()
{
list<Observer *>::iterator iter = m_observers.begin();
for(; iter != m_observers.end(); iter++) {
(*iter)->Update();
}
}
void Blog::SetStatus(string s)
{
m_status = s;
}
string Blog::GetStatus()
{
return m_status;
}
void BlogCSDN::SetStatus(string s)
{
m_status = "csdn??: " +m_name + s;
}
string BlogCSDN::GetStatus()
{
return m_status;
}
main.cpp
#include "observer.h"
#include "subject.h"
int main()
{
Blog *blog = new BlogCSDN("zy416548283");
Observer *observer = new ObserverBlog("tutupig"?? blog);
blog->Attach(observer);
blog->SetStatus("??????????????????");
blog->Notify();
delete blog;
delete observer;
return 0;
}
????????????:
????zy@zyPc:~/code/Cplus/designPattern/observer$ ./a.out
????tutupig-------csdn??: zy416548283????***????