谈一谈iOS单例模式
单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。
1、书写步骤
1)、创建类方法,返回对象实例.以shared defaultcurrent开头。
2)、创建一个全局变量用来保存对象的引用
3)、判断对象是否存在,若不存在,创建对象
2、具体单例模式的几种模式
第一种单例模式
//非线程安全写法
staticUserHelper*helper=nil;
+(UserHelper*)sharedUserHelper{
if(helper==nil){
helper=[[UserHelperalloc]init];
}
returnhelper;
}
第二种单例模式
//线程安全写法1
staticUserHelper*helper=nil;
+(UserHelper*)sharedUserHelper{
@synchronized(self){
if(helper==nil){
helper=[[UserHelperalloc]init];
}
}
returnhelper;
}
第三种单例模式    
+(void)initialize{
if([selfclass]==[UserHelperclass]){
helper=[[UserHelperalloc]init];
}
}
第四种单例模式
//线程安全写法3(苹果推荐,主要用这个)
staticUserHelper*helper=nil;
+(UserHelper*)sharedUserHelper{
staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
helper=[[UserHelperalloc]init];
});
returnhelper;
}
MRC全面实现单例写法(了解)
#import<Foundation/Foundation.h>
#import"UserHelper.h"
voidfunc(){
staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
NSLog(@"haha");
});
}
intmain(intargc,constchar*argv[]){
@autoreleasepool{
//[UserHelperlogout];
if([UserHelperisLogin]){
UserHelper*helper=[UserHelpersharedUserHelper];
NSLog(@"username=%@password=%@",helper.userName,helper.password);
}else{
charname[20];
charpwd[20];
NSLog(@"请输入用户名");
scanf("%s",name);
NSLog(@"请输入密码");
scanf("%s",pwd);
NSString*userName=[[NSStringalloc]initWithUTF8String:name];
NSString*password=[[NSStringalloc]initWithUTF8String:pwd];
if(userName&&password){
[UserHelperloginWithUserName:userNamepassword:password];
UserHelper*helper=[UserHelpersharedUserHelper];
NSLog(@"username=%@password=%@",helper.userName,helper.password);
}
}
//UserHelper*help1=[UserHelpersharedUserHelper];
//help1.userName=@"dahuan";
//help1.password=@"123456";
//NSLog(@"%p",help1);
//NSLog(@"%@",help1.userName);
//NSLog(@"%@",help1.password);
//
//
//UserHelper*help2=[UserHelpersharedUserHelper];
//help2.password=@"zxc";
//NSLog(@"%p",help2);
//NSLog(@"%@",help1.userName);
//NSLog(@"%@",help1.password);
}
return0;
}
//class.h
#import<Foundation/Foundation.h>
@interfaceUserHelper:NSObject
//1、创建类方法,返回对象实例shareddefaultcurrent
+(UserHelper*)sharedUserHelper;
@property(nonatomic,copy)NSString*userName;
@property(nonatomic,copy)NSString*password;
+(BOOL)isLogin;
+(void)loginWithUserName:(NSString*)userNamepassword:(NSString*)password;
+(void)logout;
@end
//class.m
#import"UserHelper.h"
//2、创建一个全局变量
#definePath@"/Users/dahuan/Desktop/data"
staticUserHelper*helper=nil;
@implementationUserHelper
//+(void)initialize{
//
//if([selfclass]==[UserHelperclass]){
//helper=[[UserHelperalloc]init];
//}
//}
+(UserHelper*)sharedUserHelper{
//3、判断对象是否存在,若不存在,创建对象
//线程安全
//@synchronized(self){
//
//if(helper==nil){
//helper=[[UserHelperalloc]init];
//}
//}
//gcd线程安全
staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
helper=[[UserHelperalloc]init];
});
returnhelper;
}
-(instancetype)init{
if(self=[superinit]){
NSString*data=[NSStringstringWithContentsOfFile:Pathencoding:NSUTF8StringEncodingerror:nil];
if(data){
NSArray*array=[datacomponentsSeparatedByString:@"-"];
_userName=array[0];
_password=array[1];
}
}
returnself;
}
+(BOOL)isLogin{
UserHelper*helper=[UserHelpersharedUserHelper];
if(helper.userName&&helper.password){
returnYES;
}
returnNO;
}
+(void)loginWithUserName:(NSString*)userNamepassword:(NSString*)password{
UserHelper*helper=[UserHelpersharedUserHelper];
helper.userName=userName;
helper.password=password;
NSArray*array=@[userName,password];
NSString*data=[arraycomponentsJoinedByString:@"-"];
[datawriteToFile:Pathatomically:YESencoding:NSUTF8StringEncodingerror:nil];
}
+(void)logout{
NSFileManager*fm=[NSFileManagerdefaultManager];
if([fmfileExistsAtPath:Path]){
[fmremoveItemAtPath:Patherror:nil];
}
}
@end
以上就是关于iOS单例模式的全部内容,希望对大家的学习有所帮助。
