IOS design pattern-Observer Pattern

Source: Internet
Author: User
Tags notification center

IOS design pattern-Observer Pattern

Observer mode:

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

 

When to use observer Mode

(1): changing an object requires changing other objects at the same time without knowing how many objects need to be changed.

(2): An object must notify other objects without knowing what other objects are.

(3) Two abstract types are mutually dependent. By encapsulating them in their own objects, they can be changed and reused separately.

 

Use the observer mode in the Cocoa Touch framework

 

1: Notification Type:

The nsicationicationcenter and NSNotification objects are used for one-to-many interaction, allowing the target and the observer to communicate in a coupled manner, while the two do not need to know much about them.

 

 

For example, the Model constructs a notification to be placed in the notification center:

 

NSNotification * notification = [NSNotification notificationWithName: @ "data change" object: self];

Nsicationicationcenter * notificationCenter = [NSNotificationCenter defacenter center];

[Icationicationcenter postNotification: notification];

 

Anyone who wants to subscribe to this notification must first register for himself.

[Icationcenter center addObserVer: self

Selector: @ selector (update :)

Name: @ "data change"

Object: subject];

 

2: Key-value observation

This mechanism is based on the NSKeyValueObserving informal Protocol, through which Cocoa provides an automated property observation capability for all objects that comply with the Protocol. based on informal protocols, KVC automatically observes object attributes through access.

 

 

/* Register the Observer */

[_ WalkeraddObserver: selfforKeyPath: @ ageoptions: NSKeyValueObservingOptionNewcontext: nil];

 

/* KVO function calls this function as long as the object's keyPath attribute changes */

 

-(Void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.