OC中的單例設計模式及單例的宏抽取,OC設計模式宏抽取

來源:互聯網
上載者:User

OC中的單例設計模式及單例的宏抽取,OC設計模式宏抽取

 

 

 

1 // 在一個對象需要重複使用,並且很頻繁時,可以對對象使用單例設計模式 2 // 單例的設計其實就是多alloc內部的allocWithZone下手,重寫該方法 3 4 #pragma Person.h檔案 5 6 #import <Foundation/Foundation.h> 7 @interface Person : NSObject <NSCopying,NSMutableCopying> 8 + (instancetype)sharePerson; // 給類提供一個建立單例對象的類Factory 方法 9 @end 10 11 #pragma Person.m檔案 12 13 // 14 // Person.m 15 // 單例設計 16 // 17 // Created by 康生 邱 on 15/11/26. 18 // Copyright (c) 2015年 康生 邱. All rights reserved. 19 // 20 21 #import "Person.h" 22 23 24 25 @implementation Person 26 27 + (instancetype)sharePerson 28 { 29 Person *instance = [[self alloc] init]; // 單例設計可以從alloc內的allocWithZone方法下手 30 return instance; 31 } 32 static Person *_instance = nil; 33 // 該方法決定了每次建立出來的是否是同一Block Storage空間的地址 34 + (instancetype)allocWithZone:(struct _NSZone *)zone 35 { 36 // 如果_instance 等於nil,那麼就調用父類的alocWithZone來建立一個對象、初始化後賦值給它 37 if (_instance == nil) { 38 _instance = [[super allocWithZone:zone] init]; 39 } 40 41 // 如果_instance 不等於nil,即已經指向了對象,那麼直接返回地址即可,這樣新建立的對象也指向同一個地址的對象 42 return _instance; 43 } 44 45 - (id)copyWithZone:(NSZone *)zone 46 { 47 // copy是由對象調用的,在單例中對象已存在,就不需要建立對象了,直接返回對象指標即可 48 return _instance; 49 } 50 51 - (id)mutableCopyWithZone:(NSZone *)zone 52 { 53 // mutableCopy和copy一樣,直接返回對象指標就可以 54 return _instance; 55 } 56 57 58 59 // 如果在MRC中,還需要重寫release、retain、retainCount 60 - (oneway void)release 61 { 62 // 為了保證單例對象在程式結束前不被釋放,\ 63 這裡什麼都不寫 64 } 65 - (instancetype)retain 66 { 67 // 直接返回單例對象地址 68 return _instance; 69 } 70 - (NSUInteger)retainCount 71 { 72 // 可以返回一個特殊的值,以提醒其他程式員 73 // return MAXFLOAT; 74 return UINT64_MAX; 75 } 76 77 @end 78 79 80 #pragma - 對單例模式的宏抽取 81 // 建立一個標頭檔(Singleton.h) 82 83 // 可以使用interfaceSingleton替換掉Person類單例類Factory 方法聲明 84 #define interfaceSingleton(name) +(instancetype)share##name; 85 86 // 如果當前工程在ARC下 87 #if __has_feature(objc_arc) 88 // ARC 89 #define implementationSingleton(name) \ 90 + (instancetype)share##name \ 91 {\ 92 name *instance = [[self alloc] init];\ 93 return instance;\ 94 }\ 95 static name *_instance = nil;\ 96 + (instancetype)allocWithZone:(struct _NSZone *)zone\ 97 {\ 98 if (_instance == nil) {\ 99 _instance = [[super allocWithZone:zone] init];\100 }\101 return _instance;\102 }\103 - (id)copyWithZone:(NSZone *)zone\104 {\105 return _instance;\106 }\107 - (id)mutableCopyWithZone:(NSZone *)zone\108 {\109 return _instance;\110 }111 #else112 // MRC113 #define implementationSingleton(name) \114 + (instancetype)share##name \115 {\116 name *instance = [[self alloc] init];\117 return instance;\118 }\119 static name *_instance = nil;\120 + (instancetype)allocWithZone:(struct _NSZone *)zone\121 {\122 if (_instance == nil) {\123 _instance = [[super allocWithZone:zone] init];\124 }\125 return _instance;\126 }\127 - (id)copyWithZone:(NSZone *)zone\128 {\129 return _instance;\130 }\131 - (id)mutableCopyWithZone:(NSZone *)zone\132 {\133 return _instance;\134 }\135 - (oneway void)release\136 {\137 }\138 - (instancetype)retain\139 {\140 return _instance;\141 }\142 - (NSUInteger)retainCount\143 {\144 return MAXFLOAT;\145 }146 // 結束條件編譯147 #endif148 149 150 通過將單例模式相關代碼抽取成一個標頭檔,以後在類中只要匯入待標頭檔,並傳入類名即可151 152 #pragma - 單例宏抽取後的Person.h檔案153 154 #import <Foundation/Foundation.h>155 #import "Singleton.h"156 @interface Person : NSObject <NSCopying,NSMutableCopying>157 158 interfaceSingleton(Person) // 宏定義,編譯時間會把宏名替換成+ (instancetype)sharePerson;159 // 參數的傳遞是告訴宏替換成方法聲明的時間share後面是什麼名稱160 161 @end162 163 164 #pragma - 單例宏抽取後的Person.m檔案165 166 #import "Person.h"167 168 @implementation Person169 170 // 通過宏定義,給實現檔案中的代碼取別名,在實現中直接寫好宏名+ 當前的類名就行了171 implementationSingleton(Person)172 @endView Code

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.