1.單例模式(Singleton)基本概念
- 什么是單例模式
- 單例模式即一個類的對象為唯一的一個實例對象,內存地址相同
2.單例模式的簡單實現
- 自定義
QLPlayer
類,使其成為單例- 遵守蘋果的規范,單例一般會提供
sharedXXX
或者defaultXXX
方法 - 為實現
copy
和mutableCopy
方法,遵守NSCopying
和NSMutableCopying
協議
- 遵守蘋果的規范,單例一般會提供
#import <Foundation/Foundation.h>
@interface QLPlayer : NSObject <NSCopying, NSMutableCopying>
+ (instancetype)sharedPlayer;
@end
- 類的實現
- 因為alloc方法內部會調用
allocWithZone
,故重寫allocWithZone
方法實現單例 - 調用
dispatch_once
函數,保證了在多線程同時調用的情況下,依然返回單例對象
#import "QLPlayer.h"
@implementation QLPlayer
+ (instancetype)sharedPlayer
{
QLPlayer *instance = [[self alloc] init];
return instance;
}
static QLPlayer *_instance = nil;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:zone] init];
});
return _instance;
}
- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return _instance;
}
驗證
- 用五種不同的方法創建
QLPlayer
對象,打印對象內存地址
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
QLPlayer *player1 = [QLPlayer sharedPlayer];
QLPlayer *player2 = [[QLPlayer alloc] init];
QLPlayer *player3 = [QLPlayer new];
QLPlayer *player4 = [player1 copy];
QLPlayer *player5 = [player1 mutableCopy];
NSLog(@"%p, %p, %p, %p, %p", player1, player2, player3, player4, player5);
}
@end
-
結果如下,內存地址相同,說明是單例對象!
3.單例模式宏抽取
- 用
interfaceSingleton(Name)
來代替自定義單例對象類方法的聲明
#define interfaceSingleton(Name) +(instancetype)shared##Name
- 用
implementationSingleton(Name)
來代替自定義單例對象類方法的實現
#define implementationSingleton(Name) \
+ (instancetype)shared##Name \
{ \
Name *instance = [[self alloc] init]; \
return instance; \
} \
static Name *_instance = nil; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[super allocWithZone:zone] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
注意:如果是在MRC中,還需實現以下方法:
- (instancetype)retain
{
return _player;
}
- (oneway void)release
{
//為了保證只有一個實例對象,這里do nothing
}
- (NSUInteger)retainCount
{
return MAXFLOAT; //表示這是個單例對象
}
相應的,抽取的宏中,方法的聲明部分不許改變,方法的實現部分,需改為:
#if __has_feature(objc_arc)
// 說明是ARC模式
#define implementationSingleton(Name) \
+ (instancetype)shared##Name \
{ \
Name *instance = [[self alloc] init]; \
return instance; \
} \
static Name *_instance = nil; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[super allocWithZone:zone] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
#else
// 說明是MRC模式
#define implementationSingleton(Name) \
+ (instancetype)shared##Name \
{ \
Name *instance = [[self alloc] init]; \
return instance; \
} \
static Name *_instance = nil; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[super allocWithZone:zone] init]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(NSZone *)zone \
{ \
return _instance; \
} \
- (instancetype)retain \
{ \
return _player; \
} \
- (oneway void)release \
{ \
} \
- (NSUInteger)retainCount \
{ \
return MAXFLOAT; \
}
#endif
驗證
- 自定義
QLCar
類,.h
、.m
文件如下
#import <Foundation/Foundation.h>
#import "QLSingleton.h"
@interface QLCar : NSObject
interfaceSingleton(QLCar);
@end
#import "QLCar.h"
@implementation QLCar
implementationSingleton(QLCar)
@end
- 用五種不同的方法創建
QLCar
對象,打印對象內存地址
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
QLCar *car1 = [QLCar sharedQLCar];
QLCar *car2 = [[QLCar alloc] init];
QLCar *car3 = [QLCar new];
QLCar *car4 = [car1 copy];
QLCar *car5 = [car1 mutableCopy];
NSLog(@"%p, %p, %p, %p, %p", car1, car2, car3, car4, car5);
}
@end
-
結果如下,內存地址相同,說明是單例對象!