# 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:

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:

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:

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:

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

2009年11月18日 260KB 下载

#### 上传自己的库到Cocoapods

2015-11-04 11:44:54

#### 设计模式之观察者模式 (Observer Design Pattern)

2016-04-27 19:32:17

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

2009-08-10 14:56:00

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

2014-05-22 14:37:00

#### 《Design Patterns》中文版.pdf

2012年12月07日 10.23MB 下载

#### Design Pattern----Behavioral Patterns

2014-09-10 16:55:37

#### Thinking in Patterns （中文版）

2009年01月15日 1.04MB 下载

#### java设计模式大全 Design pattern samples in Java(最经典最全的资料)

2015-06-19 13:10:58

#### Microservice_Patterns

2017年09月01日 4.54MB 下载

## 不良信息举报

Patterns in Objective-C: Observer Pattern