基于模板的主题观察者模式-我应该使用static_cast还是dynamic_cast [英] Template based Subject Observer pattern - Should I use static_cast or dynamic_cast

查看:109
本文介绍了基于模板的主题观察者模式-我应该使用static_cast还是dynamic_cast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是文章用模板实现主题/观察者模式

我做了一些修改,结果变成如下代码。

I did some modification and it became code as follow.

template <class T, class A>
class Observer {
public:
    Observer() {}
    virtual ~Observer() {}
    virtual void update(T& subject, A arg) = 0;
};

template <class T, class A>
class Subject
{
public:
    Subject() {}
    virtual ~Subject() {}

    // Take note that, we didn't make the following functions as virtual,
    // as we do not expect them to be overridden.
    void attach(Observer<T, A> &observer) {
        // Ensure no duplication.
        std::vector<Observer<T, A> *>::const_iterator iterator = std::find(observers.begin(), observers.end(), &observer);
        if (iterator == observers.end()) {
            observers.push_back(&observer);
        }
    }

    void dettach(Observer<T, A> &observer) {
        std::vector<Observer<T, A> *>::const_iterator iterator = std::find(observers.begin(), observers.end(), &observer);
        if (iterator != observers.end()) {
            observers.erase(iterator);
        }
    }

    void dettachAll() {
        observers.clear();
    }

    void notify(A arg)
    {
        std::vector<Observer<T, A> *>::const_iterator it;
        for (it = observers.begin(); it != observers.end(); it++) { 
            (*it)->update(*(static_cast<T *>(this)), arg);
        }
    }

private:
    std::vector<Observer<T, A> *> observers;
};

后来,我意识到(* it)-> update(* (static_cast< T *>(this)),arg); 有限制。例如,

Later, I realize that (*it)->update(*(static_cast<T *>(this)), arg); is having limitation. For example,

// cause compilation error in static_cast, as it cannot cast cat1 to animal.
class cat1 : public animal, public Subject<animal, int> {
public:
    virtual void speak() {
        notify(888);
    }
};

class zoo1 : public Observer<animal, int> {
public:
    zoo1() {
        c.attach(*this);
        c.speak();
    }

    virtual void update(animal& subject, int arg) {
        cout << "zoo1 received notification " << arg << endl;
    }

    cat1 c;
};

我可以通过更改 static_cast 到 dynamic_cast 但是,我不确定我是否会陷入其他陷阱?我对作者使用 static_cast 的初衷是为了确保类型安全检查

I can solve the problem by changing the static_cast to dynamic_cast. However, I am not sure whether I will fall into other traps? My guess on author original intention in having static_cast, is to ensure type safety checking during compile time.

推荐答案

您的问题来自这样一个事实,即动物不仅应该是猫,而且应该是动物,

Your problem come from the fact that animal should be the subject not only the cat,

class animal : public Subject<animal,int>
{
    ...
};

class cat1 : public animal
{
    public:
    virtual void speak() 
    {
        notify(888);
    }
};

class zoo1 : public Observer<animal, int> {
public:
    zoo1() 
    {
        c.attach(*this);
        c.speak();
    }

virtual void update(animal& subject, int arg) 
    {
        cout << "zoo1 received notification " << arg << endl;
    }

cat1 c;
};

通过此操作,每个对象都可以静态投射到动物身上。 cat1不是这种情况

By doing this every Subject are static-cast "able" to animal. This is not the case with your cat1

这篇关于基于模板的主题观察者模式-我应该使用static_cast还是dynamic_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆