zoukankan      html  css  js  c++  java
  • 设计模式(10)--观察者模式

    //10.观察者模式
    //ver1
    class StockObserver; //与 Secretary 相互依赖; 所以提前声明
    class Secretary;
    
    //股票同事类
    class StockObserver
    {
    private:
    	string _name;
    	Secretary * _ps;
    public:
    	StockObserver(string name, Secretary *p)
    	{
    		_name = name;
    		_ps = p;
    	}
    	void Update()
    	{
    		//得到前台通知,赶快采取行动。
    		//_ps->GetSecretaryAction(); 
    		//这里必须屏蔽; 这里没法编译通过, StockObserver 与 Secretary 相互耦合。
    		//解决的办法是,分别写在2个.h和.cpp文件中。并在.h文件里提前声明要引用的类,并把对象定义为指针。
    	}
    };
    
    //前台秘书类
    class Secretary
    {
    private:
    	list<StockObserver *> myList;
    	string _action;
    public:
    	//增加
    	void Attach(StockObserver * psob)
    	{
    		myList.push_back(psob);
    	}
    	//减少
    	void Detach(StockObserver * psob)
    	{
    		myList.remove(psob);
    	}
    	//通知
    	void Notify()
    	{
    		list<StockObserver *>::iterator it = myList.begin();
    		for ( ; it != myList.end(); ++it )
    		{
    			(*it)->Update();
    		}
    	}
    	//前台状态
    	void SetSecretaryAction(string strAct)
    	{
    		_action = strAct;
    	}
    	string GetSecretaryAction()
    	{
    		return _action;
    	}
    };
    
    void main1()
    {
    	Secretary *ps1 = new Secretary();
    	StockObserver *pso1 = new StockObserver("fun", ps1);
    	StockObserver *pso2 = new StockObserver("test", ps1);
    
    	//前台记下两位同事
    	ps1->Attach((pso1));
    	ps1->Attach((pso2));
    
    	//老板回来了,发通知
    	ps1->SetSecretaryAction("老板回来了");
    	ps1->Notify();
    }
    
    //10.观察者模式
    //ver2
    
    //Observer.h
    
    //前置声明
    class Subject; 
    class Boss;
    class Secretary;
    
    class Observer
    {
    protected:
    	string _name;
    	Subject * _ps;
    
    public:
    	Observer(string name, Subject * ps);
    	virtual ~Observer(void);
    public:
    	virtual void Update();
    };
    
    //看股票同事
    class StockObserver : public Observer
    {
    public:
    	StockObserver(string name, Subject * ps);
    	virtual ~StockObserver();
    
    	virtual void Update();
    };
    //看NBA同事
    class NBAObserver : public Observer
    {
    public:
    	NBAObserver(string name, Subject * ps);
    	virtual ~NBAObserver();
    
    	virtual void Update();
    };
    
    //Observer.cpp
    #include "Observer.h"
    #include "Secretary.h"
    
    Observer::Observer(string name, Subject * ps)
    {
    	_ps = ps;
    	_name = name;
    }
    
    Observer::~Observer(void)
    {
    }
    
    void Observer::Update()
    {
    	_ps->GetSecretaryAction();
    }
    
    StockObserver::StockObserver(string name, Subject * ps)
    	: Observer(name, ps)
    {}
    
    StockObserver::~StockObserver()
    {}
    
    void StockObserver::Update()
    {
    	_ps->GetSecretaryAction();
    }
    
    ///////////////////////////////
    NBAObserver::NBAObserver(string name, Subject * ps) 
    	: Observer(name, ps)
    {}
    
    NBAObserver::~NBAObserver()
    {}
    
    void NBAObserver::Update()
    {
    	_ps->GetSecretaryAction();
    }
    
    //-----------------------------------
    //Subject.h
    
    //前置声明
    class Observer;
    class StockObserver;
    class NBAObserver;
    
    //通知者接口
    class Subject
    {
    public:
    	virtual void Attach(Observer *pob);
    	virtual void Detach(Observer *pob);
    	virtual void Notify();
    	void SetSecretaryAction(string strAct);
    	string GetSecretaryAction();
    };
    
    //老板通知者
    class Boss : public Subject
    {
    private:
    	list<Observer*> myList;
    	string _action;
    
    public:
    	void Attach(Observer *pob);
    	void Detach(Observer *pob);
    	void Notify();
    	void SetSecretaryAction(string strAct);
    	string GetSecretaryAction();
    };
    
    class Secretary
    {
    private:
    	list<Observer*> myList;
    	string _action;
    
    public:
    	//增加
    	void Attach(Observer * pob);
    	//减少
    	void Detach(Observer * pob);
    	//通知
    	void Notify();
    	//前台状态
    	void SetSecretaryAction(string strAct);
    	string GetSecretaryAction();
    };
    
    //Subject.cpp
    #include "Secretary.h"
    #include "Observer.h"
    
    void Subject::Attach(Observer *pob){}
    void Subject::Detach(Observer *pob){}
    void Subject::Notify(){}
    void Subject::SetSecretaryAction(string strAct){}
    string Subject::GetSecretaryAction(){return "";}
    
    ////////////////
    
    void Boss::Attach(Observer *pob)
    {
    	myList.push_back(pob);
    }
    void Boss::Detach(Observer *pob)
    {
    	myList.remove(pob);
    }
    void Boss::Notify()
    {
    	list<Observer *>::iterator it = myList.begin();
    	for ( ; it != myList.end(); ++it )
    	{
    		(*it)->Update();
    	}
    }
    void Boss::SetSecretaryAction(string strAct)
    {
    	_action = strAct;
    }
    string Boss::GetSecretaryAction()
    {
    	return _action;
    }
    ///////////////
    void Secretary::Attach(Observer * pob)
    {
    	myList.push_back(pob);
    }
    //减少
    void Secretary::Detach(Observer * pob)
    {
    	myList.remove(pob);
    }
    //通知
    void Secretary::Notify()
    {
    	list<Observer *>::iterator it = myList.begin();
    	for ( ; it != myList.end(); ++it )
    	{
    		(*it)->Update();
    	}
    }
    //前台状态
    void Secretary::SetSecretaryAction(string strAct)
    {
    	_action = strAct;
    }
    string Secretary::GetSecretaryAction()
    {
    	return _action;
    }
    
    //-----------------
    //main.cpp
    #include "Secretary.h"
    #include "Observer.h"
    
    int main()
    {
    	//老板
    	Boss *pBoss = new Boss();
    
    	//看股票同事
    	StockObserver *pTongshi1 = new StockObserver("fun", pBoss);
    	//看NBA同事
    	NBAObserver *pTongshi2 = new NBAObserver("Test", pBoss);
    
    	pBoss->Attach(pTongshi1);
    	pBoss->Attach(pTongshi2);
    
    	pBoss->Detach(pTongshi1);
    
    	//老板回来
    	pBoss->SetSecretaryAction("我是老板我回来了!");
    	//发出通知
    	pBoss->Notify();
    	return 0;
    }
    

     

  • 相关阅读:
    可图性判定HavelHakimi定理
    并查集入门
    js数组和函数应用
    DOM用法及应用
    javascript基础知识
    表单
    PHP变量
    30天自制操作系统开发笔记——IDT中断描述符表
    《30天自制操作系统》学习笔记——汇编程序磁盘BIOS调用
    汇编指令: LGDT、LIDT、LLDT、LMSW、LOADALL、LOADALL286、LOCK、LODSB、LODSW、LODSD
  • 原文地址:https://www.cnblogs.com/sylar-liang/p/6041163.html
Copyright © 2011-2022 走看看