代码改变世界

iOS中的单例设计模式详解

2016-03-08 16:48  lvjun70  阅读(243)  评论(0)    收藏  举报

单例设计模式:

作用:主要可以让一个类创m建的对象是一个内存,可以节省内存,其对象永远市一个,如果有一些数据,整个程序都用的上,只需要使用同一份资源,我们可以保证大家访问的数据是同一份数据,一般来说工具类设计为单例模式比较合适。

下面我总结了一下ARC和MRC下的实现原理

MRC:

1.你需要重写allocWithZone方法

2.重写release,retain,retainCount,copyWithZone,MutableCopyWithZone ,init方法

3.为了掉用方便,你可以按照系统掉用方式重写一个,+(instanceType)shareMethodName方法

代码:

static id _instance = nil;

+ (id)allocWithZone:(struct _NSZone*)zone

{

  if (_instance == nil)

  {

    dispatch_once_t *onceTocken;

    dispatch_once(&once, ^{

      _instance = [super allocWitnZone:zone];

    });

  }

  return _instance;

}

- (void)init

{

   static dispatch_once_t onceToken; 

  dispatch_once(&onceToken, ^{ 

  _instace = [super init]; 

  }); 

}

- (void)shareInstacne

{

  return  [[self alloc] init];

}

- (id)retain 

return self; 

- (NSUInteger)retainCount 

return 1; 

+ (id)copyWithZone:(struct _NSZone *)zone 

return _instace; 

 

+ (id)mutableCopyWithZone:(struct _NSZone *)zone 

return _instace; 

}

ARC: 

1.你需要重写allocWithZone方法

2.重写copyWithZone,MutableCopyWithZone ,init方法

3.为了掉用方便,你可以按照系统掉用方式重写一个,+(instanceType)shareMethodName方法

 代码比上面少了retain,retainCount,release方法

当然如果你想很方法的将一个类转变成一个单例模式的话,我们可以将这两种方式封装成一个宏

Singleton.h

//// 帮助实现单例设计模式:

//// .h实现

//#define SingletonH(methodName) + (instancetype)share##methodName;

//

//// .m文件实现

//#if __has_feature(objc_arc)

//

//#define SingletonM(methodName) \

//static id _instance   = nil;\

//+ (id)allocWithZone:(struct _NSZone *)zone\

//{\

//    if (_instance == nil) {\

//        static dispatch_once_t onceToken;\

//        dispatch_once(&onceToken, ^{\

//            _instance = [super allocWithZone:zone];\

//        });\

//    }\

//    return _instance ;\

//}\

//\

//- (id)init\

//{\

//    static dispatch_once_t onceToken;\

//    dispatch_once (&onceToken, ^{\

//        _instance = [super init];\

//    });\

//    return _instance;\

//}\

//+ (instancetype)share##methodName\

//{\

//    return [[self alloc] init];\

//}\

//+ (id)copyWithZone:(struct _NSZone *)zone \

//{ \

//    return _instace; \

//} \

//\

//+ (id)mutableCopyWithZone:(struct _NSZone *)zone \

//{ \

//    return _instace; \

//}

//

//

//#else

//

//#define SingletoM(methodName ) \

//static id _instance  = nil;\

//+ (id)allocWithZone:(struct _NSZone *)zone\

//{\

//if (_instance == nil) {\

//static dispatch_once_t onceToken;\

//dispatch_once(&onceToken, ^{\

//_instance = [super allocWithZone:zone];\

//});\

//}\

//return _instance ;\

//}\

//\

//- (id)init\

//{\

//static dispatch_once_t onceToken;\

//dispatch_once (&onceToken, ^{\

//_instance = [super init];\

//});\

//return _instance;\

//}\

//+ (instancetype)share##methodName\

//{\

//return [[self alloc] init];\

//}\

//\

//- (oneway void)release\

//{\

//}\

//\

//- (instancetype)retain\

//{\

//return self;\

//}\

//- (NSUInteger)retainCount\

//{\

//return 1;\

//}\

//+ (id)copyWithZone:(struct _NSZone *)zone \

//{ \

//    return _instace; \

//} \

//\

//+ (id)mutableCopyWithZone:(struct _NSZone *)zone \

//{ \

//    return _instace; \

//}

//#endif

//

// 帮助实现单例设计模式

 

// .h文件的实现

#define SingletonH(methodName) + (instancetype)shared##methodName;

 

// .m文件的实现

#if __has_feature(objc_arc) // 是ARC

#define 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 // 不是ARC

 

#define 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]; \

} \

\

- (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

 代码如上:要注意的是:

#if __has_feature(objc_arc)是判断是否有arc的