39-单例模式

1. 单例模式:
1个类的对象,无论在何时创建也无论在什么地方创建 也无论创建多少次.创建出来的都是同1个对象.
2. 无论如何创建对象,最终都会调用alloc方法来创建对象.
1). alloc方法的内部. 其实什么都没有做,只是调用了allocWithZone:方法.
2). 实际上真正申请空间 创建对象的事情是allocWithZone:方法在做.
3. 要实现单例模式.
重写+ allocWithZone:
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static id instance = nil;
if(instance == nil)
{
instance = [super allocWithZone:zone];
}
return instance;
}
4. 单例模式的规范:
如果类是1个单例模式.要求为类提供1个类方法.来返回这个单例对象.

类方法的名称必须以 shared类名; default类名;

头文件
@interface Person : NSObject

@property(nonatomic,assign)int width;
@property(nonatomic,assign)int height;

+ (instancetype)sharedPerson;
+ (instancetype)defaultPerson;

@end

实现文件
@implementation Person

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static id instance = nil;
if(instance == nil)
{
instance = [super allocWithZone:zone];
}
return instance;
}

+ (instancetype)sharedPerson
{
return [self new];
}
+ (instancetype)defaultPerson
{
return [self new];
}

@end

5、线程安全的两种方式实现单例
@interface NetworkTools : NSObject
//单例的方法
+ (instancetype)sharedNetworkTools;
+ (instancetype)sharedNetworkToolsOnce;
@end

@implementation NetworkTools
+ (instancetype)sharedNetworkTools {
static id instance = nil;
//线程同步,保证线程安全
@synchronized(self) {
if (instance == nil) {
instance = [[self alloc] init];
}
}
return instance;
}

+ (instancetype)sharedNetworkToolsOnce {
static id instance = nil;
//dispatch_once 线程安全的
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (instance == nil) {
instance = [[self alloc] init];
}
});
return instance;
}
@end
上面两种单例实现的方式,互斥锁方式没有一次性执行线程效率快

单例模式的宏文件Singleton.h
// .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


猜你喜欢

转载自blog.csdn.net/daidaishuiping/article/details/80241992