iOS design mode-Viewer

Source: Internet
Author: User

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

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.