design pattern notes [6] - Observer

688阅读 0评论2012-12-23 hanhuili
分类:C/C++

Observer is also known as dependents or publish-subscribe. When the object changes its state, its dependents will be notified and updated automatically. The key objects in this design pattern are subject and observer. A subject can have any number of observers. All observers will be notified once the subject changes its state. The subject should have methods to attach and detach observers. The concrete observer maintains (maybe not) a reference to the subject which it is interested in.

The observer pattern lets you vary subjects and observers independently. Normally the observers don't know existence of each other. Who will trigger the notification? I prefer to the client because the service is clear here. Certainly the subject can also do it.

Observers can only subscribe some aspects of one subject. Change-manager is used to maintain  the mapping between the subjects and observers.

上一篇:design pattern notes [5] - template method
下一篇:代码整洁之道 [1]