iOS 單例(singleton,GCD,@synchronize)

iOS中單例模式的兩種創(chuàng)建方法:GCD 和 @synchronize

1.GCD的方法

  • 1.重寫allocWithZone:方法(注意不是重寫alloc方法,重寫了alloc 還是會(huì)執(zhí)行allocWithZone:)
  • 2.為需要?jiǎng)?chuàng)建単例的類創(chuàng)建一個(gè)獲取単例的類方法
  • 3.最后不要忘記重寫copyWithZone:
  • 4.<NSCopying> 沒必要寫,這邊只是為了快速敲出copyWithZone:方法
@interface JYPerson () //<NSCopying>

@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方法的宏實(shí)現(xiàn)

  • 通過宏的方式省去一些不必要的代碼
  • "" 是為了讓其預(yù)編譯指令了解是宏的內(nèi)容
// .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;\
}

  • 當(dāng)然有些為了定義単例的名字可以將參數(shù)傳入
// .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.傳統(tǒng)寫法:

  • 此方法需要注意的是線程安全(@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.注意事項(xiàng):

  • 重構(gòu)GCD方法時(shí)采用的是宏方法,估計(jì)有人也會(huì)想到(多個(gè)類想獲得單例)能否通過繼承來實(shí)現(xiàn)呢?
  • 答:是不能的,現(xiàn)在可以嘗試一下我定義兩個(gè)萬能類 teacher, student 繼承于person,然后重寫単例方法。
NSLog(@"%@ %@", [JYStudent sharedInstance], [[JYStudent alloc] init]);
NSLog(@"%@ %@", [JYTeacher sharedInstance], [[JYTeacher alloc] init]);
  • 打印了上述方法會(huì)發(fā)現(xiàn)全都是JYStudent類的對(duì)象,然后換個(gè)順序:會(huì)全都是JYTeacher類的對(duì)象,以 "static id _instance; "為例static 聲明的是一個(gè)全局變量的指針,所以指向也是同一塊地址。所以會(huì)出現(xiàn)誰在前面都是誰的對(duì)象
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容