这几天由于一直在研究智能指针,由weak_ptr解决shared_ptr的循环引用问题得到了weak_ptr是shared_ptr的观察者模式(又称发布-订阅模式)的理论。但是观察者模式是怎么回事呢。先引用一段设计模式的原文:
Intent :
Define a one-to-many dependency between objects so that when oneobject changes state, all its dependents are notified and updated automatically.
即在被观察者和观察者之间定义一个一对多的依赖关系,当被观察者状态改变时,观察者就会被通知并且更新其状态。
Applicability:
应用:
When an abstraction has two aspects, one dependent on the other.Encapsulating these aspects in separate objects lets you vary and reuse them independently.
情况一:有两个事件,一个依赖于另一个,可以分别封装这两个事件以提高变化性和重用性。
When a change to one object requires changing others, and you don't know how many objects need to be changed.
情况二:当一个对象的改变会引起其他的对象的改变,而你又不知道会影响多少个的时候。
When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you