您的位置:首页 > 其它

每天一个设计模式之Observer

2012-09-08 23:35 288 查看
原文:http://en.wikipedia.org/wiki/Observer_pattern

an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of theirmethods. It is mainly used to implement distributedevent
handling systems.

下面这个图说的相当的清楚,真是一幅图胜过千言万语呀。



例如在qq这样的即时聊天工具中,当一个用户改变自己的状态时(忙,空闲或离开),希望自己的好友都能看到。

此时就需要使用观察模式。当用户A加用户B为好友时,就将B注册为一个观察者(反之亦然)。当A的状态变化时,调用notifyObserver()。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: