談一談iOS單例模式
單例模式是一種常用的軟件設(shè)計(jì)模式。在它的核心結(jié)構(gòu)中只包含一個(gè)被稱為單例的特殊類。通過(guò)單例模式可以保證系統(tǒng)中一個(gè)類只有一個(gè)實(shí)例而且該實(shí)例易于外界訪問(wèn),從而方便對(duì)實(shí)例個(gè)數(shù)的控制并節(jié)約系統(tǒng)資源。如果希望在系統(tǒng)中某個(gè)類的對(duì)象只能存在一個(gè),單例模式是最好的解決方案。
1、書寫步驟
1)、創(chuàng)建類方法,返回對(duì)象實(shí)例.以shared default current開頭。
2)、創(chuàng)建一個(gè)全局變量用來(lái)保存對(duì)象的引用
3)、判斷對(duì)象是否存在,若不存在,創(chuàng)建對(duì)象
2、具體單例模式的幾種模式
第一種單例模式
//非線程安全寫法 static UserHelper * helper = nil; + (UserHelper *)sharedUserHelper { if (helper == nil) { helper = [[UserHelper alloc] init]; } return helper; }
第二種單例模式
//線程安全寫法1 static UserHelper * helper = nil; + (UserHelper *)sharedUserHelper { @synchronized(self) { if (helper == nil) { helper = [[UserHelper alloc] init]; } } return helper; }
第三種單例模式
+ (void)initialize { if ([self class] == [UserHelper class]) { helper = [[UserHelper alloc] init]; } }
第四種單例模式
//線程安全寫法3(蘋果推薦,主要用這個(gè)) static UserHelper * helper = nil; + (UserHelper *)sharedUserHelper { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ helper = [[UserHelper alloc] init]; }); return helper; }
MRC全面實(shí)現(xiàn)單例寫法(了解)
#import <Foundation/Foundation.h> #import "UserHelper.h" void func() { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"haha"); }); } int main(int argc, const char * argv[]) { @autoreleasepool { // [UserHelper logout]; if ([UserHelper isLogin]) { UserHelper * helper = [UserHelper sharedUserHelper]; NSLog(@"username = %@ password = %@",helper.userName,helper.password); } else { char name[20]; char pwd[20]; NSLog(@"請(qǐng)輸入用戶名"); scanf("%s",name); NSLog(@"請(qǐng)輸入密碼"); scanf("%s",pwd); NSString * userName = [[NSString alloc] initWithUTF8String:name]; NSString * password = [[NSString alloc] initWithUTF8String:pwd]; if (userName && password) { [UserHelper loginWithUserName:userName password:password]; UserHelper * helper = [UserHelper sharedUserHelper]; NSLog(@"username = %@ password = %@",helper.userName,helper.password); } } // UserHelper * help1 = [UserHelper sharedUserHelper]; // help1.userName = @"dahuan"; // help1.password = @"123456"; // NSLog(@"%p",help1); // NSLog(@"%@",help1.userName); // NSLog(@"%@",help1.password); // // // UserHelper * help2 = [UserHelper sharedUserHelper]; // help2.password = @"zxc"; // NSLog(@"%p",help2); // NSLog(@"%@",help1.userName); // NSLog(@"%@",help1.password); } return 0; } //class.h #import <Foundation/Foundation.h> @interface UserHelper : NSObject //1、創(chuàng)建類方法,返回對(duì)象實(shí)例 shared default current + (UserHelper *)sharedUserHelper; @property (nonatomic, copy) NSString * userName; @property (nonatomic, copy) NSString * password; + (BOOL)isLogin; + (void)loginWithUserName:(NSString *)userName password:(NSString *)password; + (void)logout; @end // class.m #import "UserHelper.h" //2、創(chuàng)建一個(gè)全局變量 #define Path @"/Users/dahuan/Desktop/data" static UserHelper * helper = nil; @implementation UserHelper //+ (void)initialize { // // if ([self class] == [UserHelper class]) { // helper = [[UserHelper alloc] init]; // } //} + (UserHelper *)sharedUserHelper { //3、判斷對(duì)象是否存在,若不存在,創(chuàng)建對(duì)象 //線程安全 // @synchronized(self) { // // if (helper == nil) { // helper = [[UserHelper alloc] init]; // } // } //gcd 線程安全 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ helper = [[UserHelper alloc] init]; }); return helper; } - (instancetype)init { if (self = [super init]) { NSString * data = [NSString stringWithContentsOfFile:Path encoding:NSUTF8StringEncoding error:nil]; if (data) { NSArray * array = [data componentsSeparatedByString:@"-"]; _userName = array[0]; _password = array[1]; } } return self; } + (BOOL)isLogin { UserHelper * helper = [UserHelper sharedUserHelper]; if (helper.userName && helper.password) { return YES; } return NO; } + (void)loginWithUserName:(NSString *)userName password:(NSString *)password { UserHelper * helper = [UserHelper sharedUserHelper]; helper.userName = userName; helper.password = password; NSArray * array = @[userName,password]; NSString * data = [array componentsJoinedByString:@"-"]; [data writeToFile:Path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } + (void)logout { NSFileManager * fm = [NSFileManager defaultManager]; if ([fm fileExistsAtPath:Path]) { [fm removeItemAtPath:Path error:nil]; } } @end
以上就是關(guān)于iOS單例模式的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
iOS App設(shè)計(jì)模式開發(fā)中對(duì)建造者模式的運(yùn)用實(shí)例
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對(duì)建造者模式的運(yùn)用實(shí)例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04解決JSON數(shù)據(jù)因?yàn)閚ull導(dǎo)致數(shù)據(jù)加載失敗的方法
前段時(shí)間發(fā)現(xiàn)一個(gè)問(wèn)題,當(dāng)JSON數(shù)據(jù)中有null會(huì)導(dǎo)致數(shù)據(jù)加載失敗,后來(lái)解決了,現(xiàn)在將解決方法分享給大家,有同樣問(wèn)題的朋友們可以參考。下面來(lái)一起看看吧。2016-09-09iOS實(shí)現(xiàn)點(diǎn)擊狀態(tài)欄自動(dòng)回到頂部效果詳解
在IOS開發(fā)過(guò)程中,經(jīng)常會(huì)有這種需求,需要通過(guò)點(diǎn)擊狀態(tài)欄返回到頂部,給用戶更好的體驗(yàn)效果,下面這篇文章給大家詳細(xì)介紹了實(shí)現(xiàn)過(guò)程,有需要的可以參考借鑒。2016-09-09iOS應(yīng)用開發(fā)中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的簡(jiǎn)單方法筆記
這篇文章主要介紹了iOS應(yīng)用開發(fā)中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的簡(jiǎn)單方法筆記,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-02-02iOS開發(fā)驗(yàn)證判斷語(yǔ)句之正則表達(dá)式小結(jié)
最近在公司接手重構(gòu)一個(gè)項(xiàng)目,發(fā)現(xiàn)之前的開發(fā)在驗(yàn)證格式這塊寫的太亂了,到處都有相關(guān)的驗(yàn)證代碼,所以就有了這篇文章,供自己收藏也分享給有需要的朋友們參考借鑒,下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果
本篇文章主要介紹了ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08