<i id='xqFcn'><tr id='xqFcn'><dt id='xqFcn'><q id='xqFcn'><span id='xqFcn'><b id='xqFcn'><form id='xqFcn'><ins id='xqFcn'></ins><ul id='xqFcn'></ul><sub id='xqFcn'></sub></form><legend id='xqFcn'></legend><bdo id='xqFcn'><pre id='xqFcn'><center id='xqFcn'></center></pre></bdo></b><th id='xqFcn'></th></span></q></dt></tr></i><div id='xqFcn'><tfoot id='xqFcn'></tfoot><dl id='xqFcn'><fieldset id='xqFcn'></fieldset></dl></div>

    1. <tfoot id='xqFcn'></tfoot>

    2. <small id='xqFcn'></small><noframes id='xqFcn'>

    3. <legend id='xqFcn'><style id='xqFcn'><dir id='xqFcn'><q id='xqFcn'></q></dir></style></legend>
        <bdo id='xqFcn'></bdo><ul id='xqFcn'></ul>

        C++中的观察者设计模式

        时间:2023-06-04
      1. <i id='0YEv9'><tr id='0YEv9'><dt id='0YEv9'><q id='0YEv9'><span id='0YEv9'><b id='0YEv9'><form id='0YEv9'><ins id='0YEv9'></ins><ul id='0YEv9'></ul><sub id='0YEv9'></sub></form><legend id='0YEv9'></legend><bdo id='0YEv9'><pre id='0YEv9'><center id='0YEv9'></center></pre></bdo></b><th id='0YEv9'></th></span></q></dt></tr></i><div id='0YEv9'><tfoot id='0YEv9'></tfoot><dl id='0YEv9'><fieldset id='0YEv9'></fieldset></dl></div>
          <tbody id='0YEv9'></tbody>

          <bdo id='0YEv9'></bdo><ul id='0YEv9'></ul>

            • <legend id='0YEv9'><style id='0YEv9'><dir id='0YEv9'><q id='0YEv9'></q></dir></style></legend>

                <tfoot id='0YEv9'></tfoot>

                <small id='0YEv9'></small><noframes id='0YEv9'>

                  本文介绍了C++中的观察者设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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;
                  }
                  

                  这篇关于C++中的观察者设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:返回“NULL 引用"在 C++ 中? 下一篇:在对象工厂中注册对象创建者

                  相关文章

                1. <i id='Jt7Uu'><tr id='Jt7Uu'><dt id='Jt7Uu'><q id='Jt7Uu'><span id='Jt7Uu'><b id='Jt7Uu'><form id='Jt7Uu'><ins id='Jt7Uu'></ins><ul id='Jt7Uu'></ul><sub id='Jt7Uu'></sub></form><legend id='Jt7Uu'></legend><bdo id='Jt7Uu'><pre id='Jt7Uu'><center id='Jt7Uu'></center></pre></bdo></b><th id='Jt7Uu'></th></span></q></dt></tr></i><div id='Jt7Uu'><tfoot id='Jt7Uu'></tfoot><dl id='Jt7Uu'><fieldset id='Jt7Uu'></fieldset></dl></div>
                2. <small id='Jt7Uu'></small><noframes id='Jt7Uu'>

                    <tfoot id='Jt7Uu'></tfoot>
                    <legend id='Jt7Uu'><style id='Jt7Uu'><dir id='Jt7Uu'><q id='Jt7Uu'></q></dir></style></legend>

                      <bdo id='Jt7Uu'></bdo><ul id='Jt7Uu'></ul>