标签:return define oat stat size har retain single type
什么是单例模式:(Singleton)
什么情况下使用单例?
单例设计模式的要点:
Singleton.h
// 以后就可以使用interfaceSingleton来替代后面的方法声明 #define interfaceSingleton(name) +(instancetype)share##name #if __has_feature(objc_arc) // ARC #define implementationSingleton(name) + (instancetype)share##name { name *instance = [[self alloc] init]; return instance; } static name *_instance = nil; + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[super allocWithZone:zone] init]; }); return _instance; } - (id)copyWithZone:(NSZone *)zone{ return _instance; } - (id)mutableCopyWithZone:(NSZone *)zone { return _instance; } #else // MRC #define implementationSingleton(name) + (instancetype)share##name { name *instance = [[self alloc] init]; return instance; } static name *_instance = nil; + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[super allocWithZone:zone] init]; }); return _instance; } - (id)copyWithZone:(NSZone *)zone{ return _instance; } - (id)mutableCopyWithZone:(NSZone *)zone { return _instance; } - (oneway void)release { } - (instancetype)retain { return _instance; } - (NSUInteger)retainCount { return MAXFLOAT; } #endif
// // Person.h #import <Foundation/Foundation.h> #import "Singleton.h" @interface Person : NSObject interfaceSingleton(Person); @end
// // Person.m #import "Person.h" @implementation Person implementationSingleton(Person) @end
标签:return define oat stat size har retain single type
原文地址:http://www.cnblogs.com/xufengyuan/p/6653758.html