单例设计模式的基本步骤:
- 声明一个单件对象的静态实例,并初始化为
nil
。- 创建一个类的类工厂方法,当且仅当这个类的实例为
nil
时生成一个该类的实例- 实现
NScopying
协议, 覆盖allocWithZone:
方法,确保用户在直接分配和初始化对象时,不会产 生另一个对象。- 覆盖
release
、autorelease
、retain
、retainCount
方法, 以此确保单例的状态。- 在多线程的环境中,注意使用
@synchronized
关键字或GCD
,确保静态实例被正确的创建和初始化。
MRC写法
#import "SoundTool.h"
static SoundTool * _instance = nil;
@implementation SoundTool
/**
* alloc方法内部会调用allocWithZone
* zone:系统分配给app的内存
*/
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (instancetype)init{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
});
return _instance;
}
+ (instancetype)shareSoundTool{
return [[self alloc] init];
}
- (oneway void)release{
}
- (instancetype)retain{
return self;
}
- (NSUInteger)retainCount{
return 1;
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return _instance;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return _instance;
}
@end
ARC写法
#import "SoundTool.h"
static SoundTool * _instance = nil;
@implementation SoundTool
/**
* alloc方法内部会调用allocWithZone
* zone:系统分配给app的内存
*/
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (instancetype)init{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
});
return _instance;
}
+ (instancetype)shareSoundTool{
return [[self alloc] init];
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return _instance;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return _instance;
}
@end
若需要创建多个单例可将其写成宏
使用以下语法来区分
ARC
与MRC
#if __has_feature(objc_arc) //ARC #else //MRC #endif
代码如下:
//
// Singleton.h
// LantaiyuanBus
//
// Created by lantaiyuan on 16/9/7.
// Copyright © 2016年 youmy. All rights reserved.
//
// .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
用法:
.h
文件
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface VideoTool : NSObject
SingletonH(VideoTool)
@end
.m
文件
#import "VideoTool.h"
@implementation VideoTool
SingletonM(VideoTool)
@end
网友评论