First of all, a simple mention of a single-column concept, of course, the specific description may be benevolent see!
1. Single case design mode (Singleton)
1> What is a single column: it guarantees that the object created by a class will always be only 1
2> effect (why use)
- Save Memory overhead
- If there is some data, the whole program is used, only the same resources (to ensure that the data you access are the same, consistent)
- In general, the tool design is more appropriate for a single case model
3> How to achieve, the old programmer is encountering such a problem! There's less under arc!
Cut the crap, take a look at my header file in single-case mode! Here the main is __has_feature (OBJC_ARC) judged whether it is arc, so the code looks a bit more!
Helps implement a single-case design pattern
Implementation of the. h file
Define SINGLETONH (MethodName) + (instancetype) shared# #methodName;
Implementation of the. m file
If __has_feature (OBJC_ARC)//Is arcDefine SINGLETONM (MethodName) \
Static id _instace = NIL; \
- (ID) Allocwithzone: (struct nszone *) zone \
{ \
if (instace = = nil) {\
Static dispatch_once_t Oncetoken; \
Dispatch_once (&oncetoken, ^{\
Instace = [Super Allocwithzone:zone]; \
}); \
} \
return Instace; \
} \
\
- (ID) init \
{ \
Static dispatch_once_t Oncetoken; \
Dispatch_once (&oncetoken, ^{\
Instace = [Super init]; \
}); \
return Instace; \
} \
\
- (instancetype) shared# #methodName \
{ \
return [[Self alloc] init]; \
} \
- (ID) Copywithzone: (struct nszone *) zone \
{ \
return Instace; \
} \
\
- (ID) Mutablecopywithzone: (struct nszone *) zone \
{ \
return Instace; \
}
else//Not arcDefine SINGLETONM (MethodName) \
Static id _instace = NIL; \
- (ID) allocwithzone: (struct nszone *) zone \
{\
if ( instace = = nil) {\
static dispatch_once _t Oncetoken; \
Dispatch_once (&oncetoken, ^{\
instace = [Super Allocwithzone:zone]; \
}); \
} \
Return instace; \
} \
\
- (ID) init \
{\
static dispatch_once_t oncetoken; \
Dispatch_once (&oncetoken, ^{ \
instace = [super init]; \
}; \
return instace; \
} \
\
- (instancetype) shared# #m Ethodname \
{\
return [[Self alloc] init]; \
} \
\
- (oneway void) release \
{\
\
} \
\
- (ID) retain \
{\
return self; \
} \
\
- (nsuinteger) retaincount \
{\
return 1; \
} \
- (ID) copywithzone: (struct nszone *) zone \
{\
return instace; \
} \
\
- (ID) mutablecopywithzone: (struct nszone *) zone \
{\
return instace; \
}
endif
This is completely done, single-column all considerations, including copy case, multi-threading, and automatic judging arc and MRC situation!
Use only when you include the header file and then use the following
. h file: Singletonh (Httptool)
. m file: Singletonm (Httptool)
This thing who use who know!!!! Hee Hee Hope to be helpful to the reader!