Objective-c Single case Mode singleton

Source: Internet
Author: User

List the referenced documents first:

Http://www.cnblogs.com/supercheng/archive/2012/11/26/singlemodal.html

http://arthurchen.blog.51cto.com/2483760/642536/

http://blog.csdn.net/duboleon/article/details/6337174

https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/ Cocoaobjects.html#//apple_ref/doc/uid/tp40002974-ch4-sw32

Https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Singleton.html

"Big Talk design mode"

Http://www.cocoachina.com/industry/20130510/6168.html

===============================================================================

Singleton Singleton mode: guarantees that a class has only one instance and provides a global access point to access it.

Implementing a singleton pattern requires a focus on the case where multiple instances are created by multithreaded access.

The code that does not consider multithreading is as follows:

////MyManager.h//Singleton////Created by a chess on 15-3-7.//Copyright (c) A chess of 2015 years. All rights reserved.//#import<Foundation/Foundation.h>@interfacemymanager:nsobject{NSString*_namer;} @property (nonatomic,retain) NSString*name;+(ID) Sharedmymanager;//sharedxxx,defaultxxx,currentxxx@endCarryon -/3/Ten  +: -: the////MYMANAGER.M//Singleton////Created by a chess on 15-3-7.//Copyright (c) A chess of 2015 years. All rights reserved.//#import "MyManager.h"//creating Singleton ObjectsStaticMymanager *instance;@implementationMymanager@synthesizeName =_name;+(ID) sharedmymanager{if(Instance = =Nil) {Instance=[[Self alloc]init]; }    returninstance;}@end////main.m//Singleton////Created by a chess on 15-3-7.//Copyright (c) A chess of 2015 years. All rights reserved.//#import<Foundation/Foundation.h>#import "MyManager.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Mymanager*tempobject1 =[Mymanager Sharedmymanager]; Mymanager*tempobject2 =[Mymanager Sharedmymanager]; NSLog (@"TempObject1 is%p", TempObject1); NSLog (@"TempObject2 is%p", TempObject2); }    return 0;}Output --Geneva- - xx:Wuyi:03.865singleton[752:303] TempObject1 is 0X100601C20 --Geneva- - xx:Wuyi:03.871singleton[752:303] TempObject2 is 0X100601C20Program ended with exit code:0

Consider multithreaded code, which is the recommended code:

////MyManager.h//Singleton////Created by a chess on 15-3-7.//Copyright (c) A chess of 2015 years. All rights reserved.//#import<Foundation/Foundation.h>@interfacemymanager:nsobject{NSString*_namer;} @property (nonatomic,retain) NSString*name;+(ID) Sharedmymanager;//sharedxxx,defaultxxx,currentxxx+(ID) Allocwithzone: (Nszone *) zone;-(ID) Copywithzone: (Nszone *) zone;-(ID) retain;-(Nsuinteger) retaincount;-(ID) autorelease;-(OneWayvoid) release;@end////MYMANAGER.M//Singleton////Created by a chess on 15-3-7.//Copyright (c) A chess of 2015 years. All rights reserved.//#import "MyManager.h"//creating Singleton ObjectsStaticMymanager *instance;@implementationMymanager@synthesizeName =_name;+(ID) sharedmymanager{@synchronized (self) {//The Synchronized keyword ensures that only one thread accesses the code at the same time            if(Instance = =Nil) {Instance=[[Self alloc]init]; }    }    returninstance;}+(ID) Allocwithzone: (Nszone *) zone{@synchronized (self) {if(Instance = =Nil) {Instance=[Super Allocwithzone:zone]; returninstance; }        returnNil; }}-(ID) Copywithzone: (Nszone *) zone{returnSelf//Make sure that the Copy object is unique. }-(ID) retain{returnSelf//Make sure the count is unique}-(Nsuinteger) retaincount{returnUint_max;}-(ID) autorelease{returnSelf ;}-(OneWayvoid) release{// do nothing}@end


There is also a gcd notation
/*
+ (ID) sharedmymanager{
Static dispatch_once_t once;//Ensure that once is used only once
Dispatch_once (&once,^{
if (instance = = null) {
instance = [[Self alloc]init];
})
return instance;
}
*/

Overriding these methods is to comply with the singleton rule when declaring an instance in other ways, that is, there is only one object.

Objective-c Single case Mode singleton

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.