单例模式

2016-04-11  本文已影响190人  himyfairy

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

2.单例模式的简单实现

#import <Foundation/Foundation.h>
@interface QLPlayer : NSObject <NSCopying, NSMutableCopying>
+ (instancetype)sharedPlayer;
@end

#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;
}

验证

@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.单例模式宏抽取

#define interfaceSingleton(Name) +(instancetype)shared##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

验证

#import <Foundation/Foundation.h>
#import "QLSingleton.h"
@interface QLCar : NSObject
interfaceSingleton(QLCar);
@end
#import "QLCar.h"
@implementation QLCar
implementationSingleton(QLCar)
@end
@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
上一篇下一篇

猜你喜欢

热点阅读