您的位置:首页 > 移动开发 > IOS开发

iOS开发之旅--深入讲解Singleton(单例设计模式)

2015-06-22 12:52 459 查看
singleton设计模式:

一、单例设计模式是什么?

简单讲,单例设计模式就是保证了某个类在程序中只有一个实例对象。

二、作用

1> 很直接的作用就是可以节省内存

2>可以将一些数据做到整个程序的全局共享。

3>一般工具类可以做成单例

三、实现

1)OC-MRC下:

//将实力对象定义为静态id类型
static id instance;
+ (instancetype)sharedSingle
{
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}
//重写一下方法
+ (instancetype)allocWithZone:(struct _NSZone*)zone
{
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [super allocWithZone:zone];
});
return instance;
}
<pre name="code" class="objc">- (id)init
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super init];
});
+ (id)copyWithZone:(struct _NSZone *)zone
{
return _instace;
}

+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return _instace;
}
- (instancetype)autorelease{ return instance;}- (instancetype)retain{ return instance;}- (NSUInteger)retainCount{ return 1;}


2)OC-ARC下:

//将实力对象定义为静态id类型
static id instance;
+ (instancetype)sharedSingle
{
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}
//重写一下方法
+ (instancetype)allocWithZone:(struct _NSZone*)zone
{
static dispatch_once_t once;
dispatch_once(&once, ^{
instance = [super allocWithZone:zone];
});
return instance;
}
- (id)init
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super init];
});
+ (id)copyWithZone:(struct _NSZone *)zone
{
return _instace;
}

+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return _instace;
}


arc模式下,不用手动管理内存,以下四个方法可以省略。


4 - (onewayvoid)release
5 - (instancetype)retain
6 - (instancetype)autorelease
7 - (NSUInteger)retainCount

3)Swift下:

仿写OC:

// 在 Swift 中不允许在函数中定义静态成员
static var instance: SingleTools?

static var onceToken: dispatch_once_t = 0

// 1. 提供全局的访问点
class func sharedTools() -> SingleTools {

dispatch_once(&onceToken) { () -> Void in
instance = SoundTools()
}

return instance!
}
swift特有:

// 静态区的对象只能设置一次数值
// Swift 中的的单例写法和懒加载几乎一样 `static let`
// 同样也是在第一次使用时,才会创建对象
static let sharedTools2: SingleTools = {

print("创建对象")

return SingleTools()
}()


由以上看来,Swift占据优势。简便快捷高效。

在Object-C中,可以简单有效的创建单例对象。这需要下面的操作

// .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
宏定义单例方法之后,你什么都不用考虑了,在你的单例中,你只需要一句代码就能搞定。
single_h(@“你的单例类名称”)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: