iOS單例的應用

單例設計模式的基本步驟:

  • 聲明一個單件對象的靜態(tài)實例,并初始化為nil。
  • 創(chuàng)建一個類的類工廠方法,當且僅當這個類的實例為nil時生成一個該類的實例
  • 實現(xiàn) NScopying 協(xié)議, 覆蓋 allocWithZone: 方法,確保用戶在直接分配和初始化對象時,不會產(chǎn) 生另一個對象。
  • 覆蓋 release、autoreleaseretain、retainCount 方法, 以此確保單例的狀態(tài)。
  • 在多線程的環(huán)境中,注意使用 @synchronized 關鍵字或 GCD,確保靜態(tài)實例被正確的創(chuàng)建和初始化。

MRC寫法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法內(nèi)部會調(diào)用allocWithZone
     * zone:系統(tǒng)分配給app的內(nèi)存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
    
    - (oneway void)release{
        
    }
    
    - (instancetype)retain{
        return self;
    }
    
    - (NSUInteger)retainCount{
        return 1;
    }
    
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

ARC寫法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法內(nèi)部會調(diào)用allocWithZone
     * zone:系統(tǒng)分配給app的內(nèi)存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
        
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

若需要創(chuàng)建多個單例可將其寫成宏

使用以下語法來區(qū)分 ARCMRC

#if __has_feature(objc_arc) //ARC
#else //MRC
#endif

代碼如下:

    //
    //  Singleton.h
    //  LantaiyuanBus
    //
    //  Created by lantaiyuan on 16/9/7.
    //  Copyright ? 2016年 youmy. All rights reserved.
    //
    
    // .h文件的實現(xiàn)
    #define SingletonH(methodName) + (instancetype)shared##methodName;
    
    // .m文件的實現(xiàn)
    #if __has_feature(objc_arc) // 是ARC
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #else // 不是ARC
    
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    \
    - (oneway void)release \
    { \
    \
    } \
    \
    - (id)retain \
    { \
    return self; \
    } \
    \
    - (NSUInteger)retainCount \
    { \
    return 1; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #endif

用法:

.h 文件

#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface VideoTool : NSObject
SingletonH(VideoTool)
@end

.m 文件

#import "VideoTool.h"
@implementation VideoTool
SingletonM(VideoTool)
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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