• 观察者模式


    1、概念

    所谓观察,就是一个多个对象【观察者】观察一个对象【被观察者】,当被观察者有什么行为时会触发所有观察者,观察者根据自己的实际情况完成对应的操作【观察者通常有一个虚基类,实现其接口】。

    2、例子:老公回家前发消息,妻子开始做饭,孩子停止玩游戏

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <memory>
    class ObserverInterface{
    public:
        virtual void dosomething()=0;
        virtual ~ObserverInterface(){}
    };
    
    class SubjectInterface{
    public:
        virtual void Add(ObserverInterface* obr)=0;
        virtual void Remove(ObserverInterface* obr)=0;
        virtual void Notify()=0;
    
        virtual ~SubjectInterface(){}
    };
    
    class Me:public SubjectInterface{
    public:
        void Add(ObserverInterface* obr) override{
            observers.push_back(obr);
        }
    
        void Remove(ObserverInterface* obr) override{
            auto pos=std::find(observers.begin(),observers.end(),obr);
            if(pos!=observers.end()){
                observers.erase(pos);
            }
        }
    
        void Notify() override{
            for(const auto& obs:observers){
                obs->dosomething();
            }
        }
    
    private:
        std::vector<ObserverInterface*> observers;
    };
    
    class Wife:public ObserverInterface{
    public:
        ~Wife(){std::cout << "wife relax"<<std::endl;}
        void dosomething() override{
            std::cout<<"start cooking"<<std::endl;
        }
    };
    
    class Son:public  ObserverInterface{
    public:
        ~Son(){std::cout << "son relax"<<std::endl;}
        void dosomething() override {
            std::cout<<"stop gaming"<<std::endl;
        }
    };
    
    int main(){
        Me me;
        ObserverInterface* wife=new Wife;
        ObserverInterface* son=new Son;
        me.Add(wife);
        me.Add(son);
    
        //下班了 发消息
        me.Notify();
    
        delete wife;
        delete son;
    }

    3、使用智能指针管理观察者

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <memory>
    
    class ObserverInterface {
    public:
        ~ObserverInterface(){std::cout << "ObserverInterface relax"<<std::endl;}
        virtual void dosomething()=0;
        virtual ~ObserverInterface(){}
    };
    
    using pObserverInterface=std::shared_ptr<ObserverInterface>;
    class SubjectInterface{
    public:
        virtual void Add(pObserverInterface obr)=0;
        virtual void Remove(pObserverInterface obr)=0;
        virtual void Notify()=0;
    };
    
    
    class Me : public SubjectInterface{
    public:
        void Add(pObserverInterface obr) override{
            observers.push_back(obr);
        }
    
        void Remove(pObserverInterface obr) override{
            auto pos=std::find(observers.begin(),observers.end(),obr);
            if(pos!=observers.end()){
                observers.erase(pos);
            }
        }
    
        void Notify() override{
            for(const auto& obs:observers){
                obs->dosomething();
            }
        }
    
    private:
        std::vector<pObserverInterface> observers;
    };
    
    
    class Wife : public ObserverInterface{
    public:
        ~Wife(){std::cout << "wife relax"<<std::endl;}
        void dosomething() override{
            std::cout<<"start cooking"<<std::endl;
        }
    
    };
    
    class Son : public ObserverInterface{
    public:
        ~Son(){std::cout << "son relax"<<std::endl;}
        void dosomething() override {
            std::cout<<"stop gaming"<<std::endl;
        }
    };
    
    
    int main(){
        Me me;
        auto wife = std::make_shared<Wife>();
        auto son = std::make_shared<Son>();
        me.Add(wife);
        me.Add(son);
        //下班了 发消息
        me.Notify();
    
    }

    Ps:Qt的信号与槽就是一个观察者模式,信号产生,让连接了的槽都去执行。

  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/judes/p/15622954.html
Copyright © 2020-2023  润新知