A talk on IOS single case mode _ios

Source: Internet
Author: User

Single case mode is a common software design pattern. In its core structure, there is only one special class called a single example. A single example mode can guarantee that a class in a system has only one instance and that the instance is easy to access outside, thus it is convenient to control the number of instances and save the system resources. Single mode is the best solution if you want to have only one object for a class in the system.

1. Writing steps

1), create class method, return object instance. Starts with shared default current.
2), create a global variable to hold the object's reference
3), to determine whether the object exists, if it does not exist, create objects

2. Several modes of specific single case pattern

The first single case pattern

Non-thread safe notation

static Userhelper * helper = nil;

+ (Userhelper *) Shareduserhelper {

if (helper = = nil) {

helper = [[Userhelper alloc] init];

}

 return helper;

}

The second type of single case pattern

Thread-safe notation 1

static userhelper * helper = nil;

+ (Userhelper *) shareduserhelper {

 @synchronized (self) {

  

  if (helper = = nil) {

   helper = [Userhelper alloc] INIT];

  }

 return helper;

}

The third type of single case pattern

+ (void) Initialize {

 

 if ([self class] = = [Userhelper class]) {

  helper = [[Userhelper alloc] init];

 }



The fourth type of single case pattern

Thread-safe notation 3 (Apple recommended, mainly with this)

static userhelper * helper = nil;

+ (Userhelper *) shareduserhelper {

 

static dispatch_once_t Oncetoken;

 Dispatch_once (&oncetoken, ^{

  helper = [[Userhelper alloc] init];

 });

 

 return helper;

}

MRC fully realizes single case writing (understanding)

#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 (@ "Please enter user name");

  scanf ("%s", name);

  NSLog (@ "Please enter password");

  

  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, Create class method, return object instance shared de

 

Fault 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, create a global variable #define PATH @ "/users/dahuan/desktop/data" static use

 

Rhelper * helper = nil; @implementation userhelper//+ (void) InitiaLize {////if ([self class] = = [Userhelper class]) {//helper = [[Userhelper alloc] init]; }//} + (Userhelper *) Shareduserhelper {//3, to determine if the object exists, if it does not exist, create object//thread safe//@synchronized (self) {///I

F (Helper = = nil) {//helper = [[Userhelper alloc] init];

 }//}//GCD thread safe static dispatch_once_t Oncetoken;

 Dispatch_once (&oncetoken, ^{helper = [[Userhelper alloc] init];

 

 });

return helper; }-(Instancetype) init {if (self = [super init]) {NSString * data = [NSString Stringwithcontentsoffile:pat

 H 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

This is all about the iOS single example mode, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.