观察者模式

#include"stdafx.h"
#include<iostream>
#include"vector"
#include"string"
#include"list"
using namespace std;
//被观察者对象
class Secretary;
//观察者
class PlayserObserver 
{
public:
    PlayserObserver(Secretary *secretary)
    {
        this->m_secretary = secretary;
    }

    void Updaye(string action)
    {
        cout << "action" << action << endl;
    }
private:
    Secretary *m_secretary;
};


class Secretary
{
public:
    Secretary()
    {
        m_list.clear();
    }
    void Notify(string info)
    {
        for (list<PlayserObserver *>::iterator it = m_list.begin(); it != m_list.end(); it++)
        {
          
            (*it)->Updaye(info);
        }
    }
    void setPlayserObserver(PlayserObserver *o)
    {
        m_list.push_back(o);
    }
private:
    list<PlayserObserver *>m_list;
};


void main()
{
    Secretary *secretary = NULL;
    PlayserObserver *po1 = NULL;
  
    secretary = new Secretary;
    po1 = new PlayserObserver(secretary);

    secretary->setPlayserObserver(po1);
    //secretary->setPlayserObserver(po2);

    secretary->Notify("222222");

    delete po1;
   // delete po2;
    delete secretary;
    system("pause");
}

 

 

使用面向对象的思路来写观察者模式:

#include <iostream>
#include <string>
#include <list>
using namespace std;

class Subject;
//抽象观察者
class Observer
{
protected:
    string name;
    Subject *sub;
public:
    Observer(string name, Subject *sub)
    {
        this->name = name;
        this->sub = sub;
    }
    virtual void update() = 0;
};
//具体的观察者,看股票的
class StockObserver :public Observer
{
public:
    StockObserver(string name, Subject *sub) :Observer(name, sub)
    {
    }
    void update();
};
//具体的观察者,看NBA的
class NBAObserver :public Observer
{
public:
    NBAObserver(string name, Subject *sub) :Observer(name, sub)
    {
    }
    void update();
};
//抽象通知者
class Subject
{
protected:
    list<Observer*> observers;
public:
    string action;
    virtual void attach(Observer*) = 0;
    virtual void detach(Observer*) = 0;
    virtual void notify() = 0;
};
//具体通知者,秘书
class Secretary :public Subject
{
    void attach(Observer *observer)
    {
        observers.push_back(observer);
    }
    void detach(Observer *observer)
    {
        list<Observer *>::iterator iter = observers.begin();
        while (iter != observers.end())
        {
            if ((*iter) == observer)
            {
                observers.erase(iter);
            }
            ++iter;
        }
    }
    void notify()
    {
        list<Observer *>::iterator iter = observers.begin();
        while (iter != observers.end())
        {
            (*iter)->update();
            ++iter;
        }
    }
};

void StockObserver::update()
{
    cout << name << " 收到消息:" << sub->action << endl;
    if (sub->action == "梁所长来了!")
    {
        cout << "我马上关闭股票,装做很认真工作的样子!" << endl;
    }
}

void NBAObserver::update()
{
    cout << name << " 收到消息:" << sub->action << endl;
    if (sub->action == "梁所长来了!")
    {
        cout << "我马上关闭NBA,装做很认真工作的样子!" << endl;
    }
}

int main()
{
    Subject *dwq = new Secretary(); //创建观察者<br>    //被观察的对象
    Observer *xs = new NBAObserver("xiaoshuai", dwq);
    Observer *zy = new NBAObserver("zouyue", dwq);
    Observer *lm = new StockObserver("limin", dwq);
    //加入观察队列
    dwq->attach(xs);
    dwq->attach(zy);
    dwq->attach(lm);
    //事件
    dwq->action = "去吃饭了!"; 
        dwq->notify();
    cout << endl;
    dwq->action = "梁所长来了!";
    dwq->notify();
    system("pause");
    return 0;
}

 

posted @ 2017-11-17 09:20  ye_ming  阅读(158)  评论(0编辑  收藏  举报