C++中的观察者设计模式[英] Observer design pattern in C++

本文是小编为大家收集整理的关于C++中的观察者设计模式的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

观察者的设计模式是否已经在STL中定义了(例如Java.util.Observer和Java.util.util.observable在Java中)?

推荐答案

这是一个参考实现(来自 wikipepedia ).

#include <iostream>
#include <string>
#include <map>
#include <boost/foreach.hpp>

class SupervisedString;
class IObserver{
public:
    virtual void handleEvent(const SupervisedString&) = 0;
};


class SupervisedString{ // Observable class
    std::string _str;
    std::map<IObserver* const, IObserver* const> _observers;

    typedef std::map<IObserver* const, IObserver* const>::value_type item;

    void _Notify(){
        BOOST_FOREACH(item iter, _observers){
            iter.second->handleEvent(*this);
        }
    }

public:
    void add(IObserver& ref){
        _observers.insert(item(&ref, &ref));
    }

    void remove(IObserver& ref){
        _observers.erase(&ref);
    }

    const std::string& get() const{
        return _str;
    }

    void reset(std::string str){
        _str = str;
        _Notify();
    }
};


class Reflector: public IObserver{ // Prints the observed string into std::cout
public:
    virtual void handleEvent(const SupervisedString& ref){
        std::cout<<ref.get()<<std::endl;
    }
};

class Counter: public IObserver{  // Prints the length of observed string into std::cout
    virtual void handleEvent(const SupervisedString& ref){
        std::cout<<"length = "<<ref.get().length()<<std::endl;
    }
};

int main(){

    SupervisedString str;
    Reflector refl;
    Counter    cnt;

    str.add(refl);
    str.reset("Hello, World!");
    std::cout<<std::endl;

    str.remove(refl);
    str.add   (cnt);
    str.reset("World, Hello!");
    std::cout<<std::endl;

    return 0;
}

其他推荐答案

否,但是 boost.信号2 给您类似的东西.

其他推荐答案

就我的知识而言,STL没有针对观察者模式的实现.但是,有一个关于TR2中标准库的信号/插槽的建议.

有很多图书馆为观察者模式QT库提供实施,是先驱者之一. Boost库具有实现(请参见BOOST :: SIGNALS&BOOST :: SIGNALS2).

POCO C ++库具有观察者模式的整洁实现(请参见NotificationCenter).

libsigc ++,CPP事件是提供信号/插槽实现的其他一些库.

本文地址:https://www.itbaoku.cn/post/627692.html

问题描述

Is the observer design pattern already defined in STL (Like the java.util.Observer and java.util.Observable in Java) ?

推荐答案

Here is a reference implementation (from Wikipedia).

#include <iostream>
#include <string>
#include <map>
#include <boost/foreach.hpp>

class SupervisedString;
class IObserver{
public:
    virtual void handleEvent(const SupervisedString&) = 0;
};


class SupervisedString{ // Observable class
    std::string _str;
    std::map<IObserver* const, IObserver* const> _observers;

    typedef std::map<IObserver* const, IObserver* const>::value_type item;

    void _Notify(){
        BOOST_FOREACH(item iter, _observers){
            iter.second->handleEvent(*this);
        }
    }

public:
    void add(IObserver& ref){
        _observers.insert(item(&ref, &ref));
    }

    void remove(IObserver& ref){
        _observers.erase(&ref);
    }

    const std::string& get() const{
        return _str;
    }

    void reset(std::string str){
        _str = str;
        _Notify();
    }
};


class Reflector: public IObserver{ // Prints the observed string into std::cout
public:
    virtual void handleEvent(const SupervisedString& ref){
        std::cout<<ref.get()<<std::endl;
    }
};

class Counter: public IObserver{  // Prints the length of observed string into std::cout
    virtual void handleEvent(const SupervisedString& ref){
        std::cout<<"length = "<<ref.get().length()<<std::endl;
    }
};

int main(){

    SupervisedString str;
    Reflector refl;
    Counter    cnt;

    str.add(refl);
    str.reset("Hello, World!");
    std::cout<<std::endl;

    str.remove(refl);
    str.add   (cnt);
    str.reset("World, Hello!");
    std::cout<<std::endl;

    return 0;
}

其他推荐答案

No, but Boost.Signals2 gives you something similar.

其他推荐答案

As far as my knowledge goes in C++, STL doesn't have an implementation for Observer pattern. There was a proposal for Signal/Slot for standard library in TR2 though.

There are plenty of libraries which provides implementation for Observer pattern Qt library being one of the pioneers. The boost library has an implementation (see Boost::Signals & Boost::Signals2).

The Poco C++ library has a neat implementation of the observer pattern (see NotificationCenter).

libsigc++, cpp-events are some of the other libraries that provide signal/slot implementations.