首页 > 系统 > iOS > 正文

谈一谈iOS单例模式

2020-07-26 03:20:47
字体:
来源:转载
供稿:网友

单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

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单例模式的全部内容,希望对大家的学习有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表