#pragma once
#ifndef OBSERVER_H
#define OBSERVER_H
#include <list>
typedef int STATE;
class Observer;
//发布者
// Subject 抽象基类,只需要知道Observer 基类的声明就可以了
class Subject
{
public:
Subject() : m_nSubjectState(-1){}
virtual ~Subject();
void Notify(); // 通知对象改变状态
void Attach(Observer *pObserver); // 新增对象
void Detach(Observer *pObserver); // 删除对象
// 虚函数,提供默认的实现,派生类可以自己实现来覆 基类的实现
virtual void SetState(STATE nState); // 设置状态
virtual STATE GetState(); // 得到状态
protected:
STATE m_nSubjectState; // 模拟保存Subject 状态的变量
std::list<Observer*> m_ListObserver; // 保存Observer 指针的链表
};
// ConcreateSubject 类,派生在Subject 类
class ConcreateSubject : public Subject
{
public:
ConcreateSubject() : Subject(){}
virtual ~ConcreateSubject(){}
// 派生类自己实现来覆 基类的实现
virtual void SetState(STATE nState); // 设置状态
virtual STATE GetState(); // 得到状态
};
//订阅者
// Observer 抽象基类
class Observer
{
public:
Observer() : m_nObserverState(-1){}
virtual ~Observer(){}
// 纯虚函数,各个派生类可能有不同的实现
// 通知Observer 状态发生了变化
virtual void Update(Subject* pSubject) = 0;
protected:
STATE m_nObserverState; // 模拟保存Observer 状态的变量
};
// ConcreateObserver 类派生自Observer
class ConcreateObserver : public Observer
{
public:
ConcreateObserver() : Observer(){}
virtual ~ConcreateObserver(){}
// 虚函数,实现基类提供的接口
virtual void Update(Subject* pSubject);
};
#endif
#include "StdAfx.h"
#include "observer_impl.h"
#include <iostream>
#include <algorithm>
/* ----------------------
| Subject 类成员函数的实现
|
-*/
void Subject::Attach(Observer *pObserver)
{
std::cout << "Attach an Observer:"<<pObserver<<std::endl;
m_ListObserver.push_back(pObserver);
}
void Subject::Detach(Observer *pObserver)
{
std::list<Observer*>::iterator iter;
iter = std::find(m_ListObserver.begin(), m_ListObserver.end(), pObserver);
if (m_ListObserver.end() != iter)
{
std::cout << "Detach an Observer:"<<*iter<<std::endl;
m_ListObserver.erase(iter);
}
}
void Subject::Notify()
{
std::cout <<this<<" Notify Observers's State"<<std::endl;
std::list<Observer*>::iterator iter1, iter2;
for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end(); iter1 != iter2; ++iter1)
{
(*iter1)->Update(this);
}
}
void Subject::SetState(STATE nState)
{
m_nSubjectState = nState;
}
STATE Subject::GetState()
{
return m_nSubjectState;
}
Subject::~Subject()
{
std::list<Observer*>::iterator iter1, iter2, temp;
for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end(); iter1 != iter2;)
{
temp = iter1;
++iter1;
delete (*temp);
}
m_ListObserver.clear();
}
/* ----------------------
| ConcreateSubject 类成员函数的实现
|
-*/
void ConcreateSubject::SetState(STATE nState)
{
m_nSubjectState = nState;
}
STATE ConcreateSubject::GetState()
{
return m_nSubjectState;
}
/* ----------------------
| ConcreateObserver 类成员函数的实现
|
-*/
void ConcreateObserver::Update(Subject* pSubject)
{
if (NULL == pSubject)
return;
m_nObserverState = pSubject->GetState();
std::cout << "Observer:"<<this<<" GetState:" <<m_nObserverState << std::endl;
}
// Observer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "observer_impl.h"
int _tmain(int argc, _TCHAR* argv[])
{
Observer *p1 = new ConcreateObserver; //订阅者1
Observer *p2 = new ConcreateObserver; //订阅者2
Subject* p = new ConcreateSubject; //发布者
p->Attach(p1); //添加订阅
p->Attach(p2);
p->SetState(4); //状态改变
p->Notify(); //发布通知
p->Detach(p1); //删除订阅
p->SetState(10); //状态改变
p->Notify(); //发布通知
delete p;
system("pause");
return 0;
}