單例模式

1.單例模式(Singleton)基本概念

  • 什么是單例模式
    • 單例模式即一個類的對象為唯一的一個實例對象,內存地址相同

2.單例模式的簡單實現

  • 自定義QLPlayer類,使其成為單例
    • 遵守蘋果的規范,單例一般會提供sharedXXX或者defaultXXX方法
    • 為實現copymutableCopy方法,遵守NSCopyingNSMutableCopying協議
#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
  • 結果如下,內存地址相同,說明是單例對象!


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容