The basic realization of Singleton mode
1. Lazy mode
// 1. Define a static global variable in this class to prevent access by external Extren
Static ID _instance;
/**
Static: Modifier variable
1> Modifying global variables
* Global variables are scoped to the current file only, and other files cannot be accessed with the Extren keyword
2> Modifying local variables:
* The life cycle of local variables is similar to global variables
* But cannot change scope
* Can ensure that local variables are always initialized only 1 times, in the course of the program, always only 1 minutes of memory
*/
/** 2. Rewrite the llocwithzone of this class: method to Make sure that you assign only one memory address to this class * The method is called internally by the Alloc method*/+ (ID) Allocwithzone: (struct_nszone *) zone{if(_instance = = nil) {//prevent frequent lock -up@synchronized (self) {//lock to avoid errors in multiple threads and create multiple objects if(_instance = = nil) {//prevent creation of multiple_instance =[Super Allocwithzone:zone]; } } } return_instance;}//3. Provide a shared method for the external invocation of the singleton (which is provided by the General Singleton) to ensure that only the init once +(instancetype) sharedmusictool{if(_instance = = nil) {//prevent frequent lock -up@synchronized (self) {if(_instance = = nil) {//prevent creation of multiple_instance =[[Self alloc] init]; } } } return_instance;}//4. Override Copywithzone: method to avoid creating multiple objects when using copy- (ID) Copywithzone: (Nszone *) zone{return_instance;}
2. A Hungry man mode (when the class is loaded into the OC Runtime (memory), it is called once to generate the singleton (a class will only be loaded once))
//1. Define a static global variable in this class to prevent access by external ExtrenStatic ID_instance;/** 2. Rewrite the llocwithzone of this class: method, there is no lock, because the program has just started, the thread is not loaded, there is no thread unsafe problem*/+ (ID) Allocwithzone: (struct_nszone *) zone{ if(_instance = = nil) {//prevent creation of multiple_instance =[Super Allocwithzone:zone]; } return_instance;}//3. Provide a shared method to allow the outside world to invoke this singleton (the General Singleton will provide this method)+(instancetype) sharedmusictool{return_instance;}//4. Override Copywithzone: method to avoid creating multiple objects when using copy- (ID) Copywithzone: (Nszone *) zone{return_instance;}//5. Rewrite the load class method and alloc it inside//This method is called once when the program starts and loads the class.+ (void) load{_instance=[[Self alloc] init]; }
implementing a single example with a macro definition (suitable for arc and MRC)
// create a new. h file or PCH file and copy the code in. Import in the class that needs to implement the singleton and use the macro (the corresponding class name is passed in parentheses) to
//macro definitions used in. h Files#defineHjsingletonh (name) + (instancetype) shared# #name;#if__has_feature (OBJC_ARC)//the current compiler environment is arc//macro definitions used in. m Files#defineHJSINGLETONM (name)Static ID_instace;+ (ID) Allocwithzone: (struct_nszone *) Zone {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone];}); return_instace;} +(instancetype) shared# #name {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[[Self alloc] init];}); return_instace;} - (ID) Copywithzone: (Nszone *) Zone {return_instace;}#else//the current compiler environment is MRC//macro definitions used in. m Files#defineHJSINGLETONM (name)Static ID_instace; + (ID) Allocwithzone: (struct_nszone *) Zone {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone]; }); return_instace;} +(instancetype) shared# #name {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[[Self alloc] init]; }); return_instace;} - (ID) Copywithzone: (Nszone *) Zone {return_instace;} -(OneWayvoid) Release {}- (ID) Retain {returnSelf ;} -(Nsuinteger) Retaincount {return 1;} - (ID) Autorelease {returnSelf ;}#endif