iOS浅谈单例

2018-10-10  本文已影响6人  元宝是只小肥猫

ARC

#import <Foundation/Foundation.h>

@interface Manager : NSObject<NSCopying,NSMutableCopying>
+ (instancetype)sharedManager;
@end
#import "Manager.h"

@implementation Manager
static Manager *_instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    //方式1:通过加互斥锁,来解决线程安全问题
//    @synchronized(self) {
//        if (_instance == nil) {
//            _instance = [super allocWithZone:zone];
//        }
//    }
    //方式2:通过dispatch
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (instancetype)sharedManager {
    return [[self alloc]init];
}
- (id)copyWithZone:(NSZone *)zone {
    return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}
@end

MRC

1.png
image.png
#import "Manager.h"

@implementation Manager
static Manager *_instance;
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    //方式1:通过加互斥锁,来解决线程安全问题
//    @synchronized(self) {
//        if (_instance == nil) {
//            _instance = [super allocWithZone:zone];
//        }
//    }
    //方式2:通过dispatch
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}
+ (instancetype)sharedManager {
    return [[self alloc]init];
}
- (id)copyWithZone:(NSZone *)zone {
    return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
    return _instance;
}
#if __has_feature(objc_arc)
//NSLog(@"ARC");
#else
//NSLog(@"MRC");
- (oneway void)release {
     //do nothing
}
- (instancetype)retain {
    return _instance;
}
- (NSUInteger)retainCount {
    return UINT_MAX;
}
- (instancetype)autorelease {
    return _instance;
}
#endif
@end

抽取单例

单例模式是不能使用继承的


#define SingleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)
//条件满足 ARC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}

#else
//MRC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
    return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
    return MAXFLOAT;\
}
#endif

具体使用

#import <Foundation/Foundation.h>
#import "Single.h"
@interface DownloadManager : NSObject
SingleH(Manager)
@end

#import "DownloadManager.h"

@implementation DownloadManager
SingleM(Manager)
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   DownloadManager *manager1 = [DownloadManager shareManager];
   DownloadManager *manager2 = [DownloadManager shareManager];
    NSLog(@"%p___%p",manager1,manager2);
}
上一篇下一篇

猜你喜欢

热点阅读