iOS common design Patterns-Viewer mode (iOS development)

Source: Internet
Author: User
Tags notification center

Observer mode (Observer) is also called Publish/subscribe mode (publish/subscribe)

Problem

In software design, it is often necessary to change the state of an object, which causes the state of many objects to change. This feature is obvious, reusability is strong, and the objects communicate anonymously. The Observer pattern works best.

Principle

As an example:

Consists of four roles:

1, abstract theme (subject). is a protocol that is an observer collection container that defines additions, removes, and sends notifications to all observers (corresponding to attach/detach/notifyobserver three methods respectively).

2, abstract observer (Observer). It is also a protocol that has an update method but only logs itself.

3, the specific observer (Concreteobserver). Implementation of the Observer Protocol.

4, the specific theme (ConcreteSubject). Implementation of the subject protocol.

The two protocols not only improve the reusability of the system, but also reduce the lotus-goodness.

Observer.h@protocol observer@required-(void) update; @end//Subject.h@class Observer@protocol subject@required-( void) Attach: (Observer *) observer;-(void) Detach: (Observer *) observer;-(void) notifyobservers; @end// Concreteobserver.h#import "Observer.h" @interface concreteobserver:nsobject<observer> @end// Concreteobserver.m#import "ConcreteObserver.h" @implementation concreteobserver-(void) update{NSLog ("%@", self);}    @end//Concretesubject.h#import "Subject.h" @class Observer @interface concretesubject:nsobject<subject>{ nsmutablearray* observers;} @property (nonatomic, Strong) nsmutablearray* observers;+ (concretesubject*) sharedconcretesubject; @end// Concretesubject.m#import "ConcreteSubject.h" @implementation concretesubject@synthesize observers;static ConcreteSubject *sharedconcretesubject = nil;+ (ConcreteSubject) sharedconcretesubject{static dispatch_once_t    Oncetoken;        Dispatch_once (&oncetoken, ^{sharedconcretesubject = [[Self alloc] init]; SharEdconcretesubject.observers = [[Nsmutablearray alloc] init];    }); return sharedconcretesubject;} -(void) Attach: (observer*) observer{[self.observers addobject:observer];} -(void) Detach: (observer*) observer{[self.observers removeobject:observer];}    -(void) notifyobservers{for (id obs in self.observers) {[OBS update]; }} @end

I. Notification mechanism

The whole process can say this: abstract topic (Protocol) is responsible for declaring some methods, then handing over to the specific topic to implement the method, and then the action for each observer.


Application

notification mechanism (Notification):

This should be done by example, but this is really a complex ratio. So I try to understand the process.

A typical one-to-many-object communication at the time of notification.

There are three characters in this:

1. Delivery Object (agreement): abstract Theme

2, Notification Center (nsnotificationcenter): Specific topics

3. Recipient: Viewer

Process:

1, the first recipient to register notice. (AddObserver:selector:name:object:)

2, the delivery object to the message center delivery notice. (applicationwillterminate:)

3. The Broadcasting Center broadcasts all the recipients. The recipient then passes through the (handleterminate:) method to accept the notification

4, the recipient can dismiss the message notification (removeobserver:)


Second, KVO mechanism: Notify the Observer when the object property changes, instead of sending notifications to all observers

I haven't met yet! So I can't understand! Make changes next time!




iOS common design Patterns-Viewer mode (iOS development)

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.