国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS單例模式(面試必考)

2019-11-14 18:28:30
字體:
來源:轉載
供稿:網友

?# 單例模式(面試必考)

1. 單例模式的作用

  • 可以保證在程序運行過程,一個類只有一個實例,而且該實例易于供外界訪問
  • 從而方便地控制了實例個數,并節約系統資源

2. 單例模式的使用場合

  • 在整個應用程序中,共享一份資源(這份資源只需要創建初始化1次)

3. ARC中,單例模式的實現

  • 1.在.m中保留一個全局的static的實例

    static id _instance;
  • 2.重寫allocWithZone:方法,在這里創建唯一的實例(注意線程安全)

+ (instancetype)allocWithZone:(struct _NSZone *)zone{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _instance = [super allocWithZone:zone];    });    return _instance;}
  • 3.提供1個類方法讓外界訪問唯一的實例
+ (instancetype)sharedInstance{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _instance = [[self alloc] init];    });    return _instance;}
  • 4.實現copyWithZone:方法
- (id)copyWithZone:(struct _NSZone *)zone{    return _instance;}

4.ARC中完整版(GCD)

static id _instance;+ (instancetype)allocWithZone:(struct _NSZone *)zone{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _instance = [[super allocWithZone:zone] init]; // 記住這兒是super,不能用self    });    return _instance;}+ (instancetype)sharedPerson{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        _instance = [[self alloc] init];    });    return _instance;}// copyWithZone方法的前提是必須先有一個對象- (id)copyWithZone:(NSZone *)zone{    return _instance;}
  • 在實際開發中,一個應用程序可能有多個對象是單例對象,此時我們應該把單例抽成一個宏放在一個單獨的.h文件中,千萬不能用繼承

    • 其中/表示此行跟下面一行是一起的
    • ## 表示后面是參數
    • 此時,注釋只能在上面,在右邊注釋會報錯的
// .h文件中的聲明#define SLInstanceH(name)  + (instancetype)shared##name;// .m文件的實現#define SLInstanceM(name) static id _instance; //+ (instancetype)allocWithZone:(struct _NSZone *)zone/{/    static dispatch_once_t onceToken;/    dispatch_once(&onceToken, ^{/        _instance = [[super allocWithZone:zone] init];/    });/    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;/}
  • 使用宏的時候
  • 在.m文件中
#import <Foundation/Foundation.h>#import "SLSharedInstance.h"  // 導入,定義了宏的.h文件@interface SLPerson : NSObject<NSCopying>SLInstanceH(Person)  // 傳入單例的對象@end
  • 在.h文件中
#import "SLPerson.h"@implementation SLPersonSLInstanceM(Person)@end

5. 單例模式(常規做法,非GCD)

  • 一定要加互斥鎖,不然會有線程安全問題
#import "SLPerson.h"@implementation SLPersonstatic id _instance;+ (instancetype)allocWithZone:(struct _NSZone *)zone{    @synchronized(self){ // 一定要加互斥鎖,不然會有線程安全問題        if (_instance == nil) {            _instance = [[super allocWithZone:zone] init];        }    }    return _instance;}+ (instancetype)sharedPerson{    @synchronized(self){        if (_instance == nil) {            _instance = [[self alloc] init];        }    }    return _instance;}// 要有對象才能進行copy- (id)copyWithZone:(NSZone *)zone{    return _instance;}@end

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴桥县| 焦作市| 辉南县| 平阴县| 长葛市| 朝阳区| 名山县| 易门县| 尉犁县| 崇明县| 天台县| 阿勒泰市| 湟源县| 通州区| 桂东县| 堆龙德庆县| 平泉县| 湘西| 手游| 崇左市| 临安市| 哈密市| 诸暨市| 德昌县| 台湾省| 张家口市| 茌平县| 宣恩县| 星子县| 塔河县| 漳州市| 大石桥市| 枝江市| 博白县| 临邑县| 苗栗县| 抚松县| 肥西县| 衡阳市| 吉木萨尔县| 金川县|