Single Case 01

Source: Internet
Author: User

1.

Code:

@interface Samnetworkingtool:nsobject

+ (instancetype) Sharenetworkingtool;

@end

@implementation Samnetworkingtool

static ID instance;

because of historical problems, each call to Alloc will be called first Allocwithzone

+ (instancetype) Allocwithzone: (struct _nszone *) zone{

static dispatch_once_t oncetoken;

dispatch_once (&oncetoken, ^{

//This function executes only once in the program

instance = [super Allocwithzone:zone];

});

return instance;

}

+ (ID) copywithzone: (struct _nszone *) zone{

return instance;

}

+ (instancetype) sharenetworkingtool{

return [[Selfalloc] init];

}

@end

Test:

-(void) Viewdidload {

[Super Viewdidload];

For (int i = 0; i< ; i++) {

Samnetworkingtool *tool = [Samnetworkingtool Sharenetworkingtool];

NSLog (@ "%@", tool);

}

[Selfperformselectorinbackground:@selector (Testdemo) Withobject:nil];

}

-(void) Testdemo {

Puts ("-----");

For (int i = 0; i< ; i++) {

Samnetworkingtool *tool = [Samnetworkingtool Sharenetworkingtool];

NSLog (@ "%@", tool);

}

}

Singleton implementation: ( two ways: Mutex (@synchronized (self))and one-time code (dispatch_once));

2.1 Mutex @synchronized(self):

<1>. Keep An instance of the global static in the. m file .

static ID _instance;

<2>. Override several methods (Allocwithzone: and Copywithzone:) and provide a class method for the outside world to access a unique instance.

(1) rewrite the Allocwithzone: method, Create a unique instance here ( note thread safety). This method is called inside the//alloc .

+ (instancetype) Allocwithzone: (struct _nszone *) Zone {

if (_instance = = nil) { // Prevent frequent lock - up

@synchronized(self) {

if (_instance = = nil) { // Prevent creation of multiple

_instance = [super Allocwithzone:zone];

}

}

}

return _instance;

}

(2) rewrite Copywithzone: method.

+ (ID) copywithzone: (struct _nszone *) zone

{

return _instance;

}

(3) provides 1 classes of methods to access unique instances of the outside world

+ (instancetype) Sharesingleton

{

if (!_instance) { // Prevent frequent lock - up

@synchronized(self) {

if (!_instance) { // prevents creation of multiple

_instance = [[Selfalloc] init];

}

}

}

return _instance;

}

2

Disposable Code (dispatch_once):

<1>. Keep An instance of the global static in the. m file .

static ID _instance;

<2>. Override several methods (Allocwithzone: and Copywithzone:) and provide a class method for the outside world to access a unique instance.

(1) rewrite Allocwithzone: method, Create a unique instance here ( note thread safety).

+ (ID) allocwithzone: (struct _nszone *) zone

{

static dispatch_once_t Oncetoken;

dispatch_once (&oncetoken, ^{

_instace = [super Allocwithzone:zone];

});

return _instace;

}

(2) rewrite Copywithzone: method.

+ (ID) copywithzone: (struct _nszone *) zone

{

return _instance;

}

(3) provides 1 classes of methods to access unique instances of the outside world

+ (instancetype) Sharesingleton

{

static dispatch_once_t Oncetoken;

dispatch_once (&oncetoken, ^{

_instace = [[Selfalloc] init];

});

return _instace;

}

Note : The implementation of the Singleton in ARC and MRC is slightly different . MRC's implementation of the single example is a few more memory management methods than ARC :

The MRC increases the implementation of the following methods :

-(Instancetype) retain {return self;}

-(Nsuinteger) retaincount { return 1;}

-(oneway void) Release {}

-(instancetype) autorelease { return self ;}

3. judging the current environment (ARC/MRC)

#if __has_feature (OBJC_ARC)

ARC

#else

Mrc

#endif

Note: The singleton is best to provide only one way to create a single case

such as creating a management class to access the network

#import "AFHTTPSessionManager.h"

@interface Samnetworkingtool: afhttpsessionmanager

+ (instancetype) Sharenetworkingmanager;

@end

#import "SAMNetworkingTool.h"

@implementation Samnetworkingtool

static ID _instance;

+ (instancetype) sharenetworkingmanager{

static dispatch_once_t oncetoken;

dispatch_once (&oncetoken, ^{

Nsurlsessionconfiguration *sett = [nsurlsessionconfiguration defaultsessionconfiguration];

sett.timeoutintervalforrequest = a;

Nsurl *baseurl = [nsurl urlwithstring:@ "http://c.m.163.com/nc/"];

_instance = [[samnetworkingtool alloc] initwithbaseurl: BaseUrl sessionconfiguration : sett];

});

return _instance;

}

Provides only one way to create this object,

Single Case 01

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.