Common iOS design patterns-singleton Design Patterns

Source: Internet
Author: User

Common iOS design patterns-singleton Design Patterns
Detailed description of Singleton Design Mode

Basic concepts of Singleton Design Patterns in IOS when Singleton mode is used, basic steps for creating Singleton mode in a non-ARC environment, detailed code for creating Singleton mode in an ARC environment procedure

Basic Concepts

The Singleton mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance for one class in the system and the instance is easy to access.

Singleton mode in IOS

1. if an object is created, it will consume a lot of system resources. In this case, the single-instance mode is used, because only one instance is needed, which will save the alloc time.

2. in IOS development, if many modules use the same variable, if this variable is put into the singleton class, it will become easy for all calls to access this variable. Otherwise, only one module can be passed to another, which increases risks and complexity.

Basic Steps for creating a singleton mode in a non-ARC environment

1. Declare a static instance of the singleton object and initialize it as nil
2. Declare the factory method of a class, generate an instance of this class, and only generate one
3. overwrite the allcoWithZone method to ensure that no redundant object is generated during alloc.

4. Implement the NSCopying protocol and override the release, autorelease, retain, and retainCount methods to ensure that only one instantiated object exists.

5. In a multi-threaded environment, use the @ synchronized keyword.

Code implementation in non-ARC environment
#import 
  
   @interface UserContext : NSObject@property (nonatomic,retain) NSString *username;@property(nonatomic,retain)NSString *email;+(id)sharedUserDefault;@end////  UserContext.m//  SingleDemo////  Created by andyyang on 9/30/13.//  Copyright (c) 2013 andyyang. All rights reserved.//#import "UserContext.h"static UserContext *singleInstance=nil;@implementation UserContext+(id)sharedUserDefault{    if(singleInstance==nil)    {        @synchronized(self)        {            if(singleInstance==nil)            {                singleInstance=[[[self class] alloc] init];            }        }    }    return singleInstance;}+ (id)allocWithZone:(NSZone *)zone;{ if(singleInstance==nil){    singleInstance=[super allocWithZone:zone];}    return singleInstance;}-(id)copyWithZone:(NSZone *)zone{    return singleInstance;}-(id)retain{    return singleInstance;}- (oneway void)release{}- (id)autorelease{    return singleInstance;}- (NSUInteger)retainCount{    return UINT_MAX;}@end#import 
   
    #import "UserContext.h"int main(int argc, const char * argv[]){    @autoreleasepool {       UserContext *userContext1=[UserContext sharedUserDefault];        UserContext *userContext2=[UserContext sharedUserDefault];        UserContext *userContext3=[[UserContext alloc] init];        UserContext *userContext4=[userContext1 copy];        // insert code here...        NSLog(@"Hello, World!");    }    return 0;}
   
  
Basic Steps for creating the singleton mode in the ARC environment
+ (instantClass *)sharedClient {static instantClass *_sharedClient = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{_sharedClient = [[instantClass alloc] init];});return _sharedClient;}

This method has many advantages:
1 thread security
2 meets the requirements of static analyzer
3. compatible with automatic reference count (ARC)
4. Only a small amount of code is required.

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.