在一条相切的推理线上,您可以使用现有的库来实现这一点,比如
boost::signal
让你定义一个
事件
并连接
听众
(观察员)。
// Forgive the lack of encapsulation and const-correctness to keep the example simple:
struct Animal {
boost::signal< void ( Animal& )> signal_update;
std::string name;
};
class Bird : public Animal {
public:
void rename( std::string const & n ) {
name = n;
signal_update(*this);
}
};
class Zoo
{
public:
Zoo() : bird() {
bird.signal_update.connect( boost::bind( &Zoo::an_animal_changed, this, _1 ) );
}
void an_animal_changed( Animal & a ) {
std::cout << "New name is " << a.name << std::endl;
}
Bird bird;
};
int main() {
Zoo zoo;
zoo.bird.rename( "Tweety" ); // New name is Tweety
}
这个解决方案的优点(和缺点)是它可以放松观察者和主题之间的耦合。这意味着你不能只强制
Zoo
S可以观察动物,或者
观察
具有具体的签名/名称。同时,如果您的
Animal
不知道或不关心谁在观察:
class Scientist {
public:
Scientist( Zoo & zoo )
{
zoo.bird.signal_update.connect( boost::bind( &Scientist::study, this, _1 ) );
}
void study( Animal & a ) {
std::cout << "Interesting specimen this " << a.name << std::endl;
}
};
int main() {
Zoo zoo;
Scientist pete(zoo);
zoo.bird.rename( "Tweety" ); // New name is: Tweety
// Interesting specimen this Tweety
}
注意,类型和函数名都可以通过
boost::bind
. 如果一个科学家在两个动物园工作,他甚至可以被通知改变动物属于哪个动物园:
// [skipped]: added a name to the zoo
void Scientist::work_at( Zoo & zoo ) {
zoo.bird.signal_update.connect( boost::bind( &Scientist::study, this, _1, zoo.name ) );
}
// updated signature:
void Scientist::study( Animal & a, std::string const & zoo_name )
{
std::cout << "Interesting animal " << a.name << " in zoo " << zoo_name << std::endl;
}