在C ++中实现Observer模式

时间:2013-10-20 02:57:20

标签: java c++ objective-c design-patterns observer-pattern

观察者模式在事件驱动系统中非常有用。以下是它可以用两种语言实现的方法:

爪哇

使用AOP库或字节码工程(BCEL,cglib,asm等)动态创建子类。对观察到的属性的getter或setter的任何调用都会通知任何附加的观察者。

目标C

这类似于Java - 使用isa swizzling来动态创建子类。对观察到的财产的任何调用都会通知附属观察员。有趣的是,在Objective-C中,如果删除所有观察者,我们可以在没有包装属性方法的情况下调回原始类。而在Java中,类通常会加载一次,因此您总是通知一组(可能是空的)观察者。

C ++怎么样?

由于C ++中的反映有限,因此很难使用上述方法。 C ++中的“最佳”(我的意思是典型或事实标准)方法是什么?有没有办法避免上面提到的Java和Objective-C实现中的样板代码?也许使用C ++元编程功能?

2 个答案:

答案 0 :(得分:3)

我不相信有一种方法可以使用反射在C ++中实现Observer模式。如果您不使用任何外部工具,则必须手动实现所有内容。例如,我实现了类似的东西:

#include <iostream>
#include <set>
using namespace std;

class Impl;

class ObserverBase {
public:
    virtual void propertyChanged(Impl *impl, int value) = 0;
};

class Impl {
public:
    void setProperty(int value) {
        if (m_property != value) {
            m_property = value;
            for(auto observer:m_observers) {
                observer->propertyChanged(this, value);
            }
        }
    }
    int getProperty() {
        return m_property;
    }

    void addObserver(ObserverBase *observer) {
        m_observers.insert(observer);
    }
private:
    int m_property;
    set<ObserverBase *> m_observers;
};

class Observer : public ObserverBase {
public:
    virtual void propertyChanged(Impl *impl, int value) {
        cout << "Saw new value of " << value << "!" << endl;
    }
};

int main() {
    Impl impl;
    impl.addObserver(new Observer());
    impl.setProperty(5);
}

如果要自动生成ObserverBase和Impl中的for循环,可以在编译时解析C ++。我不知道有什么能帮到你。

如果您使用的是第三方库,则可能包含可提供帮助的工具。例如,如果您使用Qt,则可以使用信号/槽来通知观察者更改。

答案 1 :(得分:1)

我编写了很多C ++代码,需要为我正在处理的一些游戏组件创建一个Observer。我需要一些东西来分发“框架开始”,“用户输入”等,作为游戏中的事件给感兴趣的各方。

我还希望它是直接的C ++,不依赖于平台或特定技术(例如boost,Qt等),因为我经常在不同的项目中构建和重用组件(以及它们背后的思想)

以下是我提出的解决方案的粗略草图:

  1. Observer是一个包含键(枚举值,而不是字符串)的单例,用于注册兴趣的主题。因为它是一个单例,所以它总是存在。
  2. 每个主题都来自一个共同的基类。基类有一个抽象的虚函数Notify(...),它必须在派生类中实现,还有一个析构函数,当它被删除时,它会从Observer中删除它(它总是可以到达)。
  3. 在Observer本身内部,如果在Notify(...)正在进行时调用Detach(...),任何分离的Subjects都会在列表中结束。
  4. 在Observer上调用Notify(...)时,它会创建Subject列表的临时副本。当它迭代它时,它将它与最近分离的它进行比较。如果目标不在其上,则在目标上调用Notify(...)。否则,它会被跳过。
  5. 观察者中的通知(...)还跟踪处理级联呼叫的深度(A通知B,C,D和D.Notify(...)触发Notify(...)呼叫到E等。)
  6. 接口最终看起来像这样:

    /* 
     The Notifier is a singleton implementation of the Subject/Observer design
     pattern.  Any class/instance which wishes to participate as an observer
     of an event can derive from the Notified base class and register itself
     with the Notiifer for enumerated events.
    
     Notifier derived classes MUST implement the notify function, which has 
     a prototype of:
    
     void Notify(const NOTIFIED_EVENT_TYPE_T& event)
    
     This is a data object passed from the Notifier class.  The structure 
     passed has a void* in it.  There is no illusion of type safety here 
     and it is the responsibility of the user to ensure it is cast properly.
     In most cases, it will be "NULL".
    
     Classes derived from Notified do not need to deregister (though it may 
     be a good idea to do so) as the base class destrctor will attempt to
     remove itself from the Notifier system automatically.
    
     The event type is an enumeration and not a string as it is in many 
     "generic" notification systems.  In practical use, this is for a closed
     application where the messages will be known at compile time.  This allows
     us to increase the speed of the delivery by NOT having a 
     dictionary keyed lookup mechanism.  Some loss of generality is implied 
     by this.
    
     This class/system is NOT thread safe, but could be made so with some
     mutex wrappers.  It is safe to call Attach/Detach as a consequence 
     of calling Notify(...).  
    
     */
    
    
    class Notified;
    
    class Notifier : public SingletonDynamic<Notifier>
    {
    public:
       typedef enum
       {
          NE_MIN = 0,
          NE_DEBUG_BUTTON_PRESSED = NE_MIN,
          NE_DEBUG_LINE_DRAW_ADD_LINE_PIXELS,
          NE_DEBUG_TOGGLE_VISIBILITY,
          NE_DEBUG_MESSAGE,
          NE_RESET_DRAW_CYCLE,
          NE_VIEWPORT_CHANGED,
          NE_MAX,
       } NOTIFIED_EVENT_TYPE_T;
    
    private:
       typedef vector<NOTIFIED_EVENT_TYPE_T> NOTIFIED_EVENT_TYPE_VECTOR_T;
    
       typedef map<Notified*,NOTIFIED_EVENT_TYPE_VECTOR_T> NOTIFIED_MAP_T;
       typedef map<Notified*,NOTIFIED_EVENT_TYPE_VECTOR_T>::iterator NOTIFIED_MAP_ITER_T;
    
       typedef vector<Notified*> NOTIFIED_VECTOR_T;
       typedef vector<NOTIFIED_VECTOR_T> NOTIFIED_VECTOR_VECTOR_T;
    
       NOTIFIED_MAP_T _notifiedMap;
       NOTIFIED_VECTOR_VECTOR_T _notifiedVector;
       NOTIFIED_MAP_ITER_T _mapIter;
    
       // This vector keeps a temporary list of observers that have completely
       // detached since the current "Notify(...)" operation began.  This is
       // to handle the problem where a Notified instance has called Detach(...)
       // because of a Notify(...) call.  The removed instance could be a dead
       // pointer, so don't try to talk to it.
       vector<Notified*> _detached;
       int32 _notifyDepth;
    
       void RemoveEvent(NOTIFIED_EVENT_TYPE_VECTOR_T& orgEventTypes, NOTIFIED_EVENT_TYPE_T eventType);
       void RemoveNotified(NOTIFIED_VECTOR_T& orgNotified, Notified* observer);
    
    public:
    
       virtual void Reset();
       virtual bool Init() { Reset(); return true; }
       virtual void Shutdown() { Reset(); }
    
       void Attach(Notified* observer, NOTIFIED_EVENT_TYPE_T eventType);
       // Detach for a specific event
       void Detach(Notified* observer, NOTIFIED_EVENT_TYPE_T eventType);
       // Detach for ALL events
       void Detach(Notified* observer);
    
       /* The design of this interface is very specific.  I could 
        * create a class to hold all the event data and then the
        * method would just have take that object.  But then I would
        * have to search for every place in the code that created an
        * object to be used and make sure it updated the passed in
        * object when a member is added to it.  This way, a break
        * occurs at compile time that must be addressed.
        */
       void Notify(NOTIFIED_EVENT_TYPE_T, const void* eventData = NULL);
    
       /* Used for CPPUnit.  Could create a Mock...maybe...but this seems
        * like it will get the job done with minimal fuss.  For now.
        */
       // Return all events that this object is registered for.
       vector<NOTIFIED_EVENT_TYPE_T> GetEvents(Notified* observer);
       // Return all objects registered for this event.
       vector<Notified*> GetNotified(NOTIFIED_EVENT_TYPE_T event);
    };
    
    /* This is the base class for anything that can receive notifications.
     */
    class Notified
    {
    public:
       virtual void Notify(Notifier::NOTIFIED_EVENT_TYPE_T eventType, const void* eventData) = 0;
       virtual ~Notified();
    
    };
    
    typedef Notifier::NOTIFIED_EVENT_TYPE_T NOTIFIED_EVENT_TYPE_T;
    

    注意:Notified类只有一个函数Notify(...)。因为void *不是类型安全的,所以我创建了其他版本的notify:

    virtual void Notify(Notifier::NOTIFIED_EVENT_TYPE_T eventType, int value); 
    virtual void Notify(Notifier::NOTIFIED_EVENT_TYPE_T eventType, const string& str);
    

    相应的Notify(...)方法被添加到Notifier本身。所有这些都使用单个函数来获取“目标列表”,然后在目标上调用适当的函数。这很好用,并使接收器不必做丑陋的演员。

    这似乎运作良好。该解决方案与源代码一起发布在网络here上。这是一个相对较新的设计,因此非常感谢任何反馈。