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

首頁 > 系統 > iOS > 正文

談一談iOS單例模式

2019-10-21 18:54:14
字體:
來源:轉載
供稿:網友
這篇文章主要和大家談一談iOS中的單例模式,單例模式是一種常用的軟件設計模式,想要深入了解iOS單例模式的朋友可以參考一下
 

單例模式是一種常用的軟件設計模式。在它的核心結構中只包含一個被稱為單例的特殊類。通過單例模式可以保證系統中一個類只有一個實例而且該實例易于外界訪問,從而方便對實例個數的控制并節約系統資源。如果希望在系統中某個類的對象只能存在一個,單例模式是最好的解決方案。

1、書寫步驟

1)、創建類方法,返回對象實例.以shared  default current開頭。
2)、創建一個全局變量用來保存對象的引用
3)、判斷對象是否存在,若不存在,創建對象

2、具體單例模式的幾種模式

第一種單例模式

//非線程安全寫法static UserHelper * helper = nil;+ (UserHelper *)sharedUserHelper {if (helper == nil) {helper = [[UserHelper alloc] init];} return helper;}

第二種單例模式

//線程安全寫法1static 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(蘋果推薦,主要用這個)static UserHelper * helper = nil;+ (UserHelper *)sharedUserHelper { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{  helper = [[UserHelper alloc] init]; });  return helper;}

MRC全面實現單例寫法(了解)

#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(@"請輸入用戶名");  scanf("%s",name);  NSLog(@"請輸入密碼");  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、創建類方法,返回對象實例 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、創建一個全局變量 #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、判斷對象是否存在,若不存在,創建對象 //線程安全// @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

以上就是關于iOS單例模式的全部內容,希望對大家的學習有所幫助。



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑河市| 高青县| 乌鲁木齐市| 吴江市| 昌图县| 嘉义市| 资阳市| 紫阳县| 扎囊县| 庄浪县| 开阳县| 赣榆县| 汉阴县| 永和县| 聂荣县| 弥勒县| 云林县| 梓潼县| 兴山县| 浪卡子县| 云林县| 林周县| 秀山| 万源市| 涪陵区| 涞水县| 澎湖县| 武邑县| 施秉县| 根河市| 嵩明县| 锡林郭勒盟| 华宁县| 肃宁县| 双柏县| 清远市| 灵武市| 常宁市| 榆树市| 霍城县| 湖南省|