iOS 单例(singleton,GCD,@synchronize)

iOS中单例模式的两种创建方法:GCD 和 @synchronize

1.GCD的方法

  • 1.重写allocWithZone:方法(注意不是重写alloc方法,重写了alloc 还是会执行allocWithZone:)
  • 2.为需要创建単例的类创建一个获取単例的类方法
  • 3.最后不要忘记重写copyWithZone:
  • 4. 没必要写,这边只是为了快速敲出copyWithZone:方法
@interface JYPerson () //

@end

@implementation JYPerson

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    }
    return _instance;
}

+ (instancetype)sharedInstance
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}


2.GCD方法的宏实现

  • 通过宏的方式省去一些不必要的代码
  • "" 是为了让其预编译指令了解是宏的内容
// .h文件
#define JYSingletonH + (instancetype)sharedInstance;

// .m文件
#define JYSingletonM \
\
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
\
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        \
        _instance = [super allocWithZone:zone];\
    });\
    return _instance;\
}\
\
+ (instancetype)sharedInstance {\
    \
    static dispatch_once_t onceToken;\
    dispatch_once(&onceToken, ^{\
        _instance = [[self alloc] init];\
    });\
    return _instance;\
}\
\
- (id)copyWithZone:(NSZone *)zone {\
    \
    return _instance;\
}

  • 当然有些为了定义単例的名字可以将参数传入
// .h文件
#define JYSingletonH(name) + (instancetype)shared##name;

// .m文件
#define JYSingletonM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}

3.传统写法:

  • 此方法需要注意的是线程安全(@synchronize)
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    @synchronized (self) {
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    }
    return _instance;
}

+ (instancetype)sharedInstance {
    @synchronized (self) {
        if (_instance == nil) {
            
            _instance = [[self alloc] init];
        }
    }
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone {
    return _instance;
}

4.注意事项:

  • 重构GCD方法时采用的是宏方法,估计有人也会想到(多个类想获得单例)能否通过继承来实现呢?
  • 答:是不能的,现在可以尝试一下我定义两个万能类 teacher, student 继承于person,然后重写単例方法。
NSLog(@"%@ %@", [JYStudent sharedInstance], [[JYStudent alloc] init]);
NSLog(@"%@ %@", [JYTeacher sharedInstance], [[JYTeacher alloc] init]);
  • 打印了上述方法会发现全都是JYStudent类的对象,然后换个顺序:会全都是JYTeacher类的对象,以 "static id _instance; "为例static 声明的是一个全局变量的指针,所以指向也是同一块地址。所以会出现谁在前面都是谁的对象

你可能感兴趣的:(iOS 单例(singleton,GCD,@synchronize))