• 观察者模式


    1】什么是观察者模式?
    
    观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。
    
    这个主题对象在状态上发生变化时,会通知所有观察者对象,让他们能够自动更新自己 
    
    【2】观察者模式代码示例:
    
    代码示例如下:
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Observer;
    
    class Subject
    {
    protected:
        list<Observer*> observers;
    public:
        string action;
    public:
        virtual void attach(Observer*) = 0;
        virtual void detach(Observer*) = 0;
        virtual void notify() = 0;
    };
    
    class Observer
    {
    protected:
        string name;
        Subject *sub;
    public:
        Observer(string name, Subject *sub)
        {
            this->name = name;
            this->sub = sub;
        }
        string getName()
        {
            return name;
        }
        virtual void update() = 0;
    };
    
    class StockObserver : public Observer
    {
    public:
        StockObserver(string name, Subject *sub) : Observer(name, sub)
        {
        }
        void update();
    };
    
    void StockObserver::update()
    {
        cout << name << " 收到消息:" << sub->action << endl;
        if (sub->action == "梁所长来了!")
        {
            cout << "我马上关闭股票,装做很认真工作的样子!" << endl;
        }
    }
    
    class NBAObserver : public Observer
    {
    public:
        NBAObserver(string name, Subject *sub) : Observer(name, sub)
        {
        }
        void update();
    };
    
    void NBAObserver::update()
    {
        cout << name << " 收到消息:" << sub->action << endl;
        if (sub->action == "梁所长来了!")
        {
            cout << "我马上关闭NBA,装做很认真工作的样子!" << endl;
        }
    }
    
    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)
                {
                    cout << "erase:" << observer->getName() << endl;
                    observers.erase(iter++);
                }
                else
                {
                    ++iter;
                }
            }
        }
        void notify()
        {
            list<Observer *>::iterator iter = observers.begin();
            while (iter != observers.end())
            {
                (*iter)->update();
                ++iter;
            }
        }
    };
    
    
    int main()
    {
        Subject *dwq = new Secretary();
    
        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();
    
        dwq->detach(lm);
        dwq->detach(zy);
        dwq->detach(xs);
    
        return 0;
    }
  • 相关阅读:
    socket.io+angular.js+express.js做个聊天应用(二)
    [原创]Python通过Thrift连接HBase
    [原创]安装Sqoop并验证
    使用PostgreSQL、Hibernate 构建 NoSQL
    [原创]HBase客户端开发举例(第三部分)
    [原创]HBase客户端开发举例(第二部…
    [原创]全分布模式下Hadoop安装
    Samba的基本配置
    常见设计模式举例 转载有改动
    【转载】hibernate中使用ehcache
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4534618.html
Copyright © 2020-2023  润新知