<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++中的观察者设计模式

        Observer design pattern in C++(C++中的观察者设计模式)
      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++中的观察者设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                  • <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>
                      <tbody id='Jt7Uu'></tbody>
                  • <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>