首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Objective-C 單例模式最佳實踐:封鎖 alloc/init/copy 確保唯一實例

     編輯:關於C語言

基本單例的缺陷

僅用 dispatch_once 回傳共享實例,無法阻止外部呼叫 [[Singleton alloc] init] 或 [singleton copy] 產生新物件。

完整防禦式單例實現

@implementation Singleton

static Singleton *_instance = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init];
    });
    return _instance;
}

// 1. 截獲 allocWithZone
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [self sharedInstance];
}

// 2. 截獲 copyWithZone
- (id)copyWithZone:(struct _NSZone *)zone {
    return [self sharedInstance];
}

// 可選:防止多次 release 導致過早釋放
- (oneway void)release { }
- (instancetype)retain { return self; }
- (NSUInteger)retainCount { return NSUIntegerMax; }

@end

驗證結果

Singleton *obj1 = [Singleton sharedInstance];
Singleton *obj2 = [Singleton sharedInstance];
Singleton *obj3 = [[Singleton alloc] init];
Singleton *obj4 = [obj1 copy];
// 所有指標指向同一記憶體位址

核心原理

  • allocWithZone: 是記憶體分配的入口,覆寫後直接回傳單例;
  • copyWithZone: 處理 copy/mutableCopy;
  • 覆寫 release/retain/retainCount 可防止手動釋放導致崩潰(ARC 環境下通常不需要)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved