IOS. Common design Patterns. 01. Singleton Mode

Source: Internet
Author: User

Classes that use Singleton mode:

UIApplication

Uiaccelerometer

Nsuserdefaults

Nsnotificationcenter

Nsfilemanager

NSBundle, etc.

Singleton.h

#import <Foundation/Foundation.h>@interface  singleton:nsobject//  Always return the same Singleton pointer + (Singleton **singletondata; @end

Singleton.m

#import "Singleton.h"@implementationSingleton@synthesizeSingletondata =_singletondata;StaticSingleton *sharemanger =Nil;/** This method uses the GCD (Grand Central Dispatch) technology, which is an opportunity for the C-language multi-threaded access count. The Dispatch_once function is provided by GCD, and its purpose is to execute only one block of code throughout the application life cycle. Dispatch_once_t is a struct provided by GCD, which is used to pass the GCD address to the Dispatch_once function. The Dispatch_once function can record whether the code is being called quickly or not. **/+ (Singleton *) sharedmanager{Staticdispatch_once_t once; Dispatch_once (&once, ^{Sharemanger=[[Self alloc] init];    }); returnSharemanger;}@end

Singleappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{     @" Singleton Init " ;         *bundle = [NSBundle mainbundle];    NSLog (@ "bundle =%@", bundle);     return YES;}

Singleviewcontroller.m

-(void) viewdidload{    [Super Viewdidload];        NSLog (@ "viewdidload Print:%@", [Singleton sharedmanager].singletondata);     *bundle = [NSBundle mainbundle];    NSLog (@ "bundle =%@", bundle);}

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.