c++ 数据封装模式之四: 观察着工厂模式
2011-06-05 14:05
295 查看
// Observer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ostream.h"
#include <afxcoll.h>
//定义抽象观察者角色
class CObserver
{
public:
//
#include "stdafx.h"
#include "ostream.h"
#include <afxcoll.h>
//定义抽象观察者角色
class CObserver
{
public:
// Observer.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "ostream.h" #include <afxcoll.h> //定义抽象观察者角色 class CObserver { public: virtual UpdateOpt()= 0; }; //定义具体观察者角色 class CDetailObsvr: public CObserver { public: virtual UpdateOpt() { cout<<"Get Notification!/n"; } }; //定义抽象主题角色 class CSubject { public: virtual void AddObsvr(CObserver* Observer) = 0; virtual void DeleteObsvr()= 0; virtual void NotifyObsvr()= 0; virtual void SetState(int State) = 0; virtual ~CSubject() { ; } }; //定义具体主题角色 class CDetailSub: public CSubject { protected: CObList* m_ObsvrList; int m_State; virtual void NotifyObsvr() { POSITION pos = m_ObsvrList->GetHeadPosition(); while (pos) { CObserver* pObsvr = (CObserver*)m_ObsvrList->GetNext(pos); pObsvr->UpdateOpt(); } } public: virtual void SetState(int State) { m_State = State; NotifyObsvr(); } CDetailSub() { m_ObsvrList = new CObList(); } virtual void AddObsvr(CObserver* Observer) { m_ObsvrList->AddTail((CObject*)Observer); } virtual void DeleteObsvr() { m_ObsvrList->RemoveTail(); } virtual ~CDetailSub() { POSITION pos = m_ObsvrList->GetHeadPosition(); if (pos) { while (pos) { CObserver* pObsvr = (CObserver*)m_ObsvrList->GetNext(pos); delete pObsvr; } m_ObsvrList->RemoveAll(); } delete m_ObsvrList; } }; int main(int argc, char* argv[]) { CSubject* pSub = new CDetailSub(); pSub->AddObsvr(new CDetailObsvr()); pSub->AddObsvr(new CDetailObsvr()); pSub->AddObsvr(new CDetailObsvr()); pSub->AddObsvr(new CDetailObsvr()); pSub->SetState(10); delete pSub; return 0; } |
相关文章推荐
- c++ 数据封装模式之一: 简单工厂模式
- c++ 数据封装模式之一: 简单工厂模式
- c++ 数据封装模式之二: 工厂方法模式
- 运用反射原理的简单工厂模式和运用反射原理从数据库里读出数据直接封装到实体集合里
- c++ 数据封装模式之三: 抽象工厂模式
- 工厂模式加单例管理Fragment,同时解决fragment切换数据丢失问题
- c++设计模式之工厂模式
- C++设计模式-Factory工厂模式
- C++设计模式-Factory工厂模式
- 设计模式 -- 简单工厂模式 -- c++实现
- C++设计模式-Factory工厂模式
- 设计模式----工厂模式(c++)
- c++ 设计模式之简单的工厂模式
- C++设计模式之一 工厂模式(简单工厂、工厂和抽象工厂)
- C++设计模式--工厂模式
- C++ 数据抽象、数据封装与接口(抽象类)
- 使用简单工厂模式和反射机制优化MVP数据绑定流程
- C++设计模式之工厂方法模式
- C++ 数据封装和抽象
- 设计模式的C++实现 24.简单工厂模式