我指的是文章 Implementing a Subject/Observer pattern with templates
我做了一些修改,变成了如下代码。
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);
有限制。例如,
// 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
, 是为了确保在编译时进行类型安全检查。
最佳答案
你的问题来自于动物不仅应该是猫的主题,
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 不是这种情况
关于c++ - 基于模板的主题观察者模式——我应该使用 static_cast 还是 dynamic_cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4341693/