OC Singleton Mode

Source: Internet
Author: User

In iOS development, Singleton is one of the most useful design patterns. It is the most useful way to share data between codes without passing parameters manually. For more information about Singleton and other design patterns, see this book: "Cocoa design pattern" background Singleton is an important concept, which is an extremely convenient design model. A large number of Singleton concepts are used in the iPhone SDK. For example, the sharedApplication method of UIApplication returns a UIApplication instance of the current application at any time. How to implement a singleton class using the following code: MyManager. h # import <foundation/Foundation. h> @ interfaceMyManager: NSObject {NSString * someProperty;} @ property (nonatomic, retain) NSString * someProperty; + (id) sharedManager; @ end MyManager. m # import "MyManager. h "@ implementationMyManager @ synthesizesomeProperty; # pragmamark Singleton Methods + (id) sharedManager {static MyManager * sharedMyManager = nil; static dispatch_once_to NceToken; dispatch_once (& onceToken, ^ {sharedMyManager = [[self alloc] init] ;}); return sharedMyManager;}-(id) init {if (self = [super init]) {someProperty = [[NSString alloc] initWithString: @ "Default Property Value"] ;}return self ;}- (void) dealloc {// shocould never be called, but justhere for clarity really .} @ end we define a static variable named sharedMyManager, Which is instantiated only once in the sharedManager method. Using the dispath_once method of GCD, we ensure that the sharedMyManager method is created only once. This is thread-safe, so you don't have to worry about it. However, if you do not want to use GCG, you can also implement the sharedManager method as follows: non-GCD code + (id) sharedManager {@ synchronized (self) {if (sharedMyManager = nil) sharedMyManager = [[self alloc] init];} returnsharedMyManager;} calls the singleton object: MyManager * sharedManager = [MyManager sharedManager]. In my code, this code is used in many places. I use these Singleton objects to process CoreLocation or CoreData. Non-ARC Code if you do not use ARC (not recommended), you should use the following code: MyManager. h (non-ARC) # import "MyManager. h "static MyManager * sharedMyManager = nil; @ implementation MyManager @ synthesize someProperty; # pragma mark Singleton Methods + (id) sharedManager {@ synchronized (self) {if (sharedMyManager = nil) sharedMyManager = [[super allocWithZone: NULL] init];} return sharedMyManager;} + (id) allocWithZone :( NSZone *) zone {return [self sharedManager] retain];} -(id) copyWithZone :( NSZone *) zone {return self;}-(id) retain {return self;}-(unsigned) retainCount {return UINT_MAX; // denotes anobject that cannot be released}-(oneway void) release {// never release}-(id) autorelease {return self;}-(id) init {if (self = [super init]) {someProperty = [[NSString alloc] initWithString: @ "Default PropertyValue"];} return self;}-(void) dealloc {// shocould never be called, but just here for clarity really. [someProperty release]; [super dealloc] ;}@ end

Related Article

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.