Patterns in Objective-C: Observer Pattern

原创 2009年06月04日 00:22:00

Patterns in Objective-C: Observer Pattern

Posted 12/14/2008 - 18:16 by David

One of the most common patterns, after the Singleton, in Java is the Observer Pattern. Also referred to as a Broadcaster/Listener or a Publish/Subscribe pattern. The basic principle is that there are two components: An Observer (or Listener) and a Subject (or Broadcaster).

The basic idea is that a listener registers with a broadcaster using some predefined protocol. At some later point, the broadcaster is told to notify all of its listeners, where it calls some function on each of its listeners and passes certain arguments along. This allows for asynchronous message passing between two different objects that don't have to know about one-another, they just have to know about the broadcaster.

Examples of where this gets used are to track database updates (i.e., listeners want to know when the database updates in specific ways), handle click events, or implement shutdown hooks.

Apple has provided an Observer Pattern in the Cocoa library called the NSNotificationCenter. There are three primary tasks that are performed with the NSNotificationCenter:

Adding a Observer
  1. [[NSNotificationCenter defaultCenter] addObserver:listener selector:@selector(notify:) name:@"Event" object:notificationSendert];

Here we see several components:

  • [NSNotificationCenter defaultCenter] - While one can create their own NSNotificationCenters, for most purposes it is sufficient to use the default.
  • addObserver:listener - listener is the object that will be notified by the NSNotificationCenter. For many applications the observer will "self register" with the observer, and thus the call will be addObserver:self.
  • selector:@selector(notify:) - Indicates which method should be called when the observer is notified.
  • name:@"Event" - The name of the event that will cause the NSNotificationCenter to notify the observer. It is a good practice to use an extern string constant instead of directly inserting the string.
  • object:notificationSender - The notificationSender represents the object that will post the notification. This is frequently used when the object in question needs to be released once a notification happens.

This is frequently nil, meaning that the NSNotificationCenter will not consider the sender when deciding whether to fire the event. If it is not nil, then only notifications sent from that specific object will fire the event (see below).

Note that if name is left as nil then all notifications sent by the notificationSender will fire the event.

The notify function can be any function that takes a single argument:

 

  1. - (void)notify:(NSNotification *)notification {
  2.         id notificationSender = [notification object];
  3.         //do stuff
  4. }

 

Notifying the Observers
In some cases, such as when registering for events with Cocoa's built-in libraries (e.g., MPMoviePlayerController) the notification event is called by the library when a certain condition is met, for example when the movie ends.

In other cases, we will want to trigger that event:

  1. [[NSNotificationCenter defaultCenter] postNotificationName:@"Event" object:notificationSender];

 

The components here are similar to when we added the observer above. The postNotificationName is identical to the name, and the object is the notificationSender.

Removing an Observer
When we are done with the observer, we will want to unregister it with the NSNotificationCenter:

 

  1. [[NSNotificationCenter defaultCenter] removeObserver:listener name:@"Event" object:notificationSender];

 

The arguments here are the same as when we added the observer.

设计模式 - 观察者模式(Observer Pattern) 详解

观察者模式(Observer Pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157 版权...
  • u012515223
  • u012515223
  • 2014年05月22日 14:37
  • 2982

Design Patterns - Observer Pattern

Observer pattern is used when there is one-to-many relationship between objects such as if one objec...
  • aqzwss
  • aqzwss
  • 2016年09月06日 15:28
  • 219

理解观察者模式(Observer Pattern)必看的文章

1.       说明:观察者模式是软件设计的一种模式,也叫做发布/订阅模式。日常生活中基本上都有用到它。比如使用MSN当你的一个好友上线时,你就会收到上线通知;开心网当某个人转帖后,他的好友就都可以...
  • dujingjing1230
  • dujingjing1230
  • 2009年08月10日 14:56
  • 3499

设计模式总结之Composite Pattern(组合模式)

组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。...
  • cooldragon
  • cooldragon
  • 2016年08月11日 00:49
  • 1157

上传自己的库到Cocoapods

上传自己的库到Cocoapods注册trunk先执行以下2句指令:sudo gem install cocoapods pod trunk register myw_720@163.com 'Yawe...
  • keleyundou
  • keleyundou
  • 2015年11月04日 11:44
  • 7886

C++版的Head First Design Pattern

       前 些天从sourceforge上down下一个C++版的Head First Design Pattern,一直都没有仔细看过,也不知道HeadFirstDesignPattern是一...
  • iinel
  • iinel
  • 2007年11月16日 10:13
  • 1580

Logstash学习5_[logstash/patterns/grok-patterns]Logstash grok 内置正则

USERNAME [a-zA-Z0-9._-]+ USER %{USERNAME} INT (?:[+-]?(?:[0-9]+)) BASE10NUM (?[+-]?(?:(?:[0-9]+(?:\...
  • wang_zhenwei
  • wang_zhenwei
  • 2015年11月10日 17:08
  • 1879

设计模式总结之Observer Pattern(观察者模式)

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。...
  • cooldragon
  • cooldragon
  • 2016年08月11日 00:53
  • 966

正则表达式 python3.x (pattern模式)

什么是正则表达式:记录文本规则的代码,不是python中特有的功能,是一种通用的方法。 1.1、没有特殊符号,只有基本字母或数字的完全匹配。例如:匹配文本中“is”   import re tex...
  • tigerking1017
  • tigerking1017
  • 2016年05月09日 13:48
  • 657

观察者模式 Observer Pattern

之前总是想写一个程序,大体上是这样的:单服务器,多客户端。服务器上比如说是个房屋价格信息,客户端则是租房子,显示价格。如果现在有特价,怎么客户端怎么实时获取?而且作为客户端,用户定是不止一个,如何通知...
  • haimian520
  • haimian520
  • 2016年04月13日 15:10
  • 3454
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Patterns in Objective-C: Observer Pattern
举报原因:
原因补充:

(最多只允许输入30个字)