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.

相关文章推荐

Objective-C中的Observer模式实现

文章出处 在我们的iOS开发中,很幸运,Cocoa架构中已经提供了消息中心组件来实现Observer模式。NSNotificationCenterNSNotificationCenter 是 Coco...

Apress.Pro.Objective-C.Design.Patterns.for.iO

  • 2013年12月01日 16:11
  • 26.28MB
  • 下载

Objective-C Singleton Pattern

根据该文章写了一篇关于iOS单例模式的文章:http://duckrowing.com/2010/05/21/using-the-singleton-pattern-in-objective-c/ 单...

Programming in Objective-C 4th

  • 2013年10月22日 21:29
  • 4.63MB
  • 下载

Programming in Objective-C 2.0 2.pdf

  • 2011年12月22日 17:44
  • 12.98MB
  • 下载

Head First Design Patterns 阅读笔记之二: Observer Pattern

从一款天气预报 APP 开始假设现在需要编写一款 APP,可以显示感应器传来的当前天气条件(温度、湿度、气压),报告当前的天气状况统计数据并做简单的预测。现在已知以下条件: WeatherData 类...

Progrmming in Objective-C 2.0

  • 2012年10月27日 14:17
  • 3.86MB
  • 下载

Programming in Objective-C

  • 2013年05月24日 13:38
  • 8.71MB
  • 下载

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
  • 192

Programming in Objective-C

  • 2016年03月05日 15:59
  • 18.66MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Patterns in Objective-C: Observer Pattern
举报原因:
原因补充:

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