Common IOS design modes-Observer mode (IOS development)

Source: Internet
Author: User

Common IOS design modes-Observer mode (IOS development)

The Observer mode (Observer) is also called the Publish/Subscribe mode (Publish/Subscribe)

-Problem:

In software design, it is often necessary to change the state of an object, which leads to changes in the state of many objects. This feature is obvious, highly reusable, and anonymous communication between objects. The observer mode is most suitable.

-Principle:

For example:

Includes four roles:

1. Abstract The topic (subject ). A protocol is a collection of observer containers. It defines three methods: add, remove, and send notifications to all observers (attach, detach, and notifyObserver ).

2. Abstract Observer (Observer ). It is also a protocol. There is an update method, but it only processes its own logs.

3. The specific observer (ConcreteObserver ). The specific implementation of the Observer protocol.

4. The specific subject (ConcreteSubject ). The specific implementation of the Subject protocol.

The two protocols not only improve the reusability of the system, but also reduce the coupling degree.

// 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
 
  @end// ConcreteObserver.m#import "ConcreteObserver.h"@implementation ConcreteObserver-(void)update{    NSLog("%@", self);}@end// ConcreteSubject.h#import "Subject.h"@class Observer;@interface ConcreteSubject : NSObject
  
   {    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 be said as follows: Abstract topics (Protocols) are responsible for declaring some methods, then handing them over to specific topics for implementation, and then performing operations on each observer.


-Application:

Notification mechanism ):

This should be done through instances, but it is really complicated. So I try to understand this process.

Typical one-to-Multiple object communication during notification.

There are three roles:

1. Delivery object (Protocol): Abstract topic

2. notification center (nsicationicationcenter): specific subject

3. Recipient: Observer

Process:

1. First, the recipients register the notification. (AddObserver: selector: name: object :)

2. send a notification to the message center. (ApplicationWillTerminate :)

3. the broadcast center broadcasts all recipients. Then the receiver accepts the notification through the (handleTerminate :) method.

4. The recipient can remove the Message notification (removeObserver :)


Ii. KVO mechanism: Notify the observer when the object property changes, instead of sending notifications to all observers.

I have never met! So I cannot understand it! Modify it next time!




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.