iOS design mode-Viewer
Schematic diagram
Description
1. The cocoa framework itself implements the observer pattern (notification hubs and KVO)
2. I have written an example, the implementation of the Notification Center, the special place is that you do not have to remove the subscribed to the notification object
Source
Https://github.com/YouXianMing/ObserverPattern
////SubscriptionServiceCenter.h//Observerpattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import "SubscriptionServiceCenterProtocol.h"/** * Subscription Service Center (implement the system's Notification Center business logic) * = NOTE = No consideration when sending notifications, synchronous and asynchronous issues **/@interfaceSubscriptionservicecenter:nsobject/** * Create a subscription number * * @param subscriptionnumber Subscription number*/+ (void) Createsubscriptionnumber: (NSString *) Subscriptionnumber;/** * Remove the subscription number (all customers participating in the subscription number will no longer receive subscription information) * * @param subscriptionnumber Subscription number*/+ (void) Removesubscriptionnumber: (NSString *) Subscriptionnumber;/** * Remove the specified customer from the specified subscription number * * @param Customer Object * @param subscriptionnumber subscription number*/+ (void) Removecustomer: (ID<SubscriptionServiceCenterProtocol>) Customer Fromsubscriptionnumber: (NSString *) Subscriptionnumber;/** * Notification of the signed subscription number of the object * * @param message Information Object * @param subscriptionnumber subscription number*/+ (void) SendMessage: (ID) Message Tosubscriptionnumber: (NSString *) Subscriptionnumber;/** * Customer Subscription specified subscription number * * @param Customer Object * @param subscriptionnumber subscription number*/+ (void) Addcustomer: (ID<SubscriptionServiceCenterProtocol>) Customer Withsubscriptionnumber: (NSString *) Subscriptionnumber;@end
////SUBSCRIPTIONSERVICECENTER.M//Observerpattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "SubscriptionServiceCenter.h"StaticNsmutabledictionary *_subscriptionnumberdictionary =Nil;@implementationSubscriptionservicecenter#pragmaMark-Initialize + (void) Initialize {if(self = = [Subscriptionservicecenterclass]) {_subscriptionnumberdictionary=[Nsmutabledictionary dictionary]; }}+ (void) Createsubscriptionnumber: (NSString *) Subscriptionnumber {nsparameterassert (subscriptionnumber); Nshashtable*hashtable =[self existsubscriptionnumber:subscriptionnumber]; if(HashTable = =Nil) {hashTable=[nshashtable weakobjectshashtable]; [_subscriptionnumberdictionary setobject:hashtable Forkey:subscriptionnumber]; }}+ (void) Removesubscriptionnumber: (NSString *) Subscriptionnumber {nsparameterassert (subscriptionnumber); if([self existsubscriptionnumber:subscriptionnumber]) {[_subscriptionnumberdictionary Removeobjectforke Y:subscriptionnumber]; }}+ (void) Removecustomer: (ID<SubscriptionServiceCenterProtocol>) Customer Fromsubscriptionnumber: (NSString *) Subscriptionnumber {nsparameterassert (subscriptionnumber); Nshashtable*hashtable =[self existsubscriptionnumber:subscriptionnumber]; if(HashTable &&customer) {[HashTable Removeobject:customer]; }}+ (void) SendMessage: (ID) Message Tosubscriptionnumber: (NSString *) Subscriptionnumber {nsparameterassert (subscriptionnumber); Nshashtable*hashtable =[self existsubscriptionnumber:subscriptionnumber]; if(hashTable) {Nsenumerator*enumerator =[hashTable Objectenumerator]; ID<SubscriptionServiceCenterProtocol> customer =Nil; while(Customer =[Enumerator Nextobject]) { if([Customer Respondstoselector: @selector (subscriptionmessage:subscriptionnumber:)]) {[Customer SUBSCRI Ptionmessage:message Subscriptionnumber:subscriptionnumber]; } } }}+ (void) Addcustomer: (ID) Customer Withsubscriptionnumber: (NSString *) Subscriptionnumber {nsparameterassert (customer); Nsparameterassert (Subscriptionnumber); Nshashtable*hashtable =[self existsubscriptionnumber:subscriptionnumber]; [HashTable Addobject:customer];}#pragmaMark-Private Method + (Nshashtable *) Existsubscriptionnumber: (NSString *) Subscriptionnumber {return[_subscriptionnumberdictionary objectforkey:subscriptionnumber];}@end
////SubscriptionServiceCenterProtocol.h//Observerpattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@protocolSubscriptionservicecenterprotocol <NSObject>/** * received subscription information * * @param message subscription information * @param subscriptionnumber subscription number*/- (void) Subscriptionmessage: (ID) Message Subscriptionnumber: (NSString *) Subscriptionnumber;@end
Analysis
Having an object that follows a certain set of protocols (interfaces) to accomplish a particular function is an important skill in cocoa development.
Subscription and the complete process for posting information on subscription numbers
iOS design mode-Viewer