• Observer


    #ifndef TEST_OBSERVER_SAFE
    #define TEST_OBSERVER_SAFE
    
    #include <stdio.h>
    #include <vector>
    #include <boost/shared_ptr.hpp>
    #include <boost/weak_ptr.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/thread/mutex.hpp>
    #include <boost/thread/lock_guard.hpp>
    
    namespace TestObserver
    {
    	class Observable;
    	class Observer : public boost::enable_shared_from_this<Observer>
    	{
    	public:
    		Observer() {}
    		virtual ~Observer() {printf("%p is destructed!
    ", this);}
    		virtual void update() = 0;
    		void observe(Observable* vSub);
    	protected:
    		Observable* m_Suject;
    	};
    
    	class Observable
    	{
    	public:
    		Observable() {}
    		~Observable() {printf("Observable is destructed!
    ");}
    		void registerd(boost::weak_ptr<Observer> vObserver)
    		{
    			m_Observers.push_back(vObserver);
    		}
    
    		void notifyObservers()
    		{
    			boost::lock_guard<boost::mutex> Lock(m_Mutex);
    			for (Iterator it = m_Observers.begin(); it != m_Observers.end(); /*++it陷阱*/)
    			{
    				boost::shared_ptr<Observer> tmpObs(it->lock());
    				if (tmpObs)
    				{
    					tmpObs->update();
    					++it;
    				}
    				else
    				{
    					printf("notifyObervers erase()
    ");
    					it = m_Observers.erase(it);
    				}
    			}
    		}
    
    	private:
    		boost::mutex m_Mutex;
    		std::vector<boost::weak_ptr<Observer>> m_Observers;
    		typedef std::vector<boost::weak_ptr<Observer>>::iterator Iterator;
    	};
    
    	void Observer::observe(Observable* vSub)
    	{
    		vSub->registerd(shared_from_this());
    		m_Suject = vSub;
    	}
    
    	class Foo : public Observer
    	{
    	public:
    		virtual void update()
    		{
    			printf("Foo::update() %p
    ", this);
    		}
    	};
    
    	void fun_1()
    	{
    		flg_str("TestObserver::fun_1()");
    		Observable subject;
    		{
    			boost::shared_ptr<Foo> p(new Foo);
    			p->observe(&subject);
    			subject.notifyObservers();
    		}
    		subject.notifyObservers();
    	}
    }
    #endif
    

      

  • 相关阅读:
    Linux 服务器连接远程数据库(Mysql、Pgsql)
    oracle主键自增
    全排列算法实现
    python动态导入包
    python发红包实现
    CentOS 6.8安装Oracle 11 g 解决xhost: unable to open display
    xargs的一个小坑
    利用ssh-copy-id复制公钥到多台服务器
    redhat 5 更换yum源
    【原创】Hadoop的IO模型(数据序列化,文件压缩)
  • 原文地址:https://www.cnblogs.com/forgood/p/3647269.html
Copyright © 2020-2023  润新知