A single example of ios-design pattern

Source: Internet
Author: User

A singleton pattern is a class that creates only one instance of the entire program's life cycle.

Implementation process:

//Singclass.h#import<Foundation/Foundation.h>@interfaceSingclass:nsobject@property (nonatomic,strong) NSString*name;+ (ID) share_id;@end//SINGCLASS.M#import "Singclass.h"@implementationSingclass+ (ID) share_id{StaticSingclass *singclass =Nil; Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Singclass=[[Singclass alloc]init];    }); returnSingclass;}@end

This method uses the time GCD technique, dispatch_once is the function that GCD provides, it executes only once in the code block's statement in the entire program life cycle, uses this kind of method to create the single-session automatic thread synchronization, is safe under the multithreading.

The purpose of using a singleton:

1, shared resources (properties)

2, sharing method

3, reduce multiple creation of classes (optimize performance)

System Single Example:

such as uiapplication, Nsfilemanager,nsbundle, Nsnotificationcenter, Nsuserdefaults.

This article Githu address Https://github.com/zhangkiwi/iOS_SN_Singclass

A single example of ios-design pattern

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.