IOS Singleton mode

Source: Internet
Author: User

It is mainly used for resource sharing control of the application. Many uses??

In essence, a singleton is a class that has and has been instantiated only once in the program declaration cycle. To ensure that the instantiation is unique, use the class's Class (static) method to generate and access the object.

At this point, you can access the class's singleton object anywhere in the program, because only the unique one is instantiated, so the Alloc, INIT, Autorelease initialization methods are not used.

1. Implement a static instance for Singleton Object, initialize it, and set it to nil;

2. Implement an instance construction method (usually named Sharedinstance or Sharedmanager) to check if the static instance declared above is nil, and if so, create a new instance of this class;

3. Rewrite the Allocwithzone: method to ensure that when other people use alloc and Init to try to obtain a new instance, no new instance is generated.

4. Appropriate implementation of Allocwithzone, Copywithzone, Release and Autorelease.

Sinleton.h header File

  singleton.h//  singleton////  Created by Leondun on 11-4-20.//  Copyright Leondun. All rights reserved.//#import <Foundation/Foundation.h>  @interface singleton:nsobject {} + (Singleton *) shar Edinstancemethod; @end

SINGLETON.M implementation file

#import "Singleton.h" static Singleton * sharedinstance = nil; @implementation Singleton//Get a single case + (Singleton *) sharedinstancemethod{    @synchronized (self) {        if ( Sharedinstance = = nil)        sharedinstance = [[Self alloc] init];}    }    return sharedinstance;} The only time Alloc Singleton, and then return nil+ (ID) Allocwithzone: (Nszone *) zone{    @synchronized (self) {        if (sharedinstance = = nil) {            instance = [Super Allocwithzone:zone];            return instance;        }    }    return nil;} Copy returns the singleton itself-(ID) Copywithzone: (Nszone *) zone{return to    self;}//retain returns the Singleton itself-(ID) retain{    return,}// The reference count is always 1-(unsigned) retaincount{    return 1;}//release does no processing-(void) release{   }//autorelease returns the Singleton itself-(ID) autorelease{    return self;}//-(void) dealloc{      [Super Dealloc];} @end

Some code descriptions:

1. Synchronized this is primarily a multi-threaded program, which can limit the code within {} to one thread, and if a thread is not finished executing,

Other threads have to wait if they need to be executed.

2. Allocwithzone this is overloaded because this is an instance of reading information from the developed memory area, so if the required singleton already has,

It is necessary to prohibit modification of the current singleton, so return nil.

What is 3.allocWithZone, Copywithzone and Nszone?

Simply put, you can imagine a memory pool, alloc or dealloc these operations, and so on in this memory pool. Cocoa always assigns a default Nszone,

Any default memory operation is performed on this "Zone".

There is a flaw in using the default Nszone because it is global in scope and frequently used too much, which inevitably leads to fragmentation of memory. Especially when using a lot of alloc and Dealloc,

Performance will receive a certain impact. To do this, you can create a nszone yourself, and limit the alloc,copy to a "Zone".

Note: Thread safety issues are not considered here.

The 1.Singleton itself is thread safe.

The instance of 2.Singleton is thread safe.

Thread safety issues are not discussed here, donuts.

IOS Singleton mode

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.