boost signal Observer pattern

boost signal 的观察者模式(Observer pattern):
函数指针是解耦对象关系的最佳利器。
Signal(如boost的signal和glib中的signal)机制是一个典型的例子,一个对象自身的状态可能是在变化的(或者会触发一些事件),
而其它对象关心它的变化。一旦该对象有变化发生,其它对象要执行相应的操作。

如果该对象直接去调用其它对象的函数,功能是完成了,但对象之间的耦合太紧了。

如何把这种耦 合降到最低呢,signal机制是很好的办法。它的原理大致如下:其它关注该对象变化的对象主动注册一个回调函数到该对象中。
一旦该对象有变化发生,就调 用这些回调函数通知其它对象。功能同样实现了,但它们之间的耦合度降低了。

http://www.scottcollins.net/articles/a-deeper-look-at-signals-and-slots.html

"Signals and Slots" are a generalized implementation of the Observer pattern.

A signal is an observable event, or at least notification that the event happened.

A slot is a potential observer, typically in the form a function to be called.

You connect a signal to a slot to establish the observable-observer relationship.

发表评论


0.013 sec