Singleton mode in objective-C

Source: Internet
Author: User

? ? The Singleton mode is a simple design mode. The design mode is not only for some programming language, but also for other OOP languages such as C ++, Java, and PHP, I first came into contact with the design model through the "design model. This book is written in Java. I personally feel that although I have not understood this book, I have gained a lot. As mentioned above, dependency injection is not easy to understand when controlling the flip. It is easy to understand strut, spring, and hibernate. However, in the 23 design modes, the singleton mode is quite understandable. How does one express the singleton mode in oc? The following uses the lusashi code to understand the singleton mode in OC.

? ? First, we need to know what the singleton mode is. In the White Paper, Singleton Mode means that this class corresponds to only one instance in the program. This is the singleton mode, the Singleton mode is generally implemented using a global static object. Next we will create a singletonclass to generate a singletonclass, and define various methods in the implementation file to implement our singletonclass mode.

 

? ? 1. The Singleton mode is generally implemented using a global static object. Therefore, defining a static global variable in singletonclass. m is indispensable.

12 // Define static global variablesstatic SingletonClass *single = nil;

 

? ? 2. the above static variables are defined in the implementation file, so they are private. To obtain an instance of this class, you must have a getinstance method to obtain the instance, before allocating memory space to static variables, you must first determine whether the memory space has been allocated. Make sure that the single instance is not allocated if it has been allocated.

123456789 // Obtain a static Global Object+(id)getInstance{    // If no object is generated, allocate memory for the static global variable    if (single == nil) {        single = [[SingletonClass alloc] init];    }    return single;}

? ?

? ? 3. To Prevent Users From instantiating objects through alloc and new, we need to rewrite the class method allcowithzone.

12345678 // Prevent creating new objects through alloc or new. We need to rewrite allocwithzone.+(id)allocWithZone:(NSZone *)zone{    if (single == nil) {        single = [[super allocWithZone:zone] init];    }    return single;}

 

? ? 4. To prevent users from copying a single instance in depth, We need to rewrite the copywithzone method and the mutablecopywithzone method. Before rewriting, our Singleton class must follow the protocol nscoping and nsmutablecoping.

? ? The following is the protocol-compliant code:

123456789 @interface SingletonClass : NSObject<NSCopying, NSMutableCopying> // How to obtain a singleton object in a singleton+(id) getInstance; // Singleton Test Method-(void) singletonFunction; @end

 

? ? Override the copywithzone Method

12345 // To prevent the creation of new instances through copy, we need to rewrite copywithzone;-(id)copyWithZone:(NSZone *)zone{    return self;}

 

? ? Override the mutablecopywithzone Method

1234 -(id)mutableCopyWithZone:(NSZone *)zone{    return self;}

 

? 5. Prevent the user from setting the created Singleton dealloc. We need to override the retaincount method.

12345 // Override the retaincount method to prevent dealloc and return the maximum value.-(NSUInteger) retainCount{    return NSUIntegerMax;}

 

? 6. Rewrite the release, autorelease, and retain methods.

12345678910111213141516 // Rewrite retain, and the reference count remains unchanged-(id) retain{    return self;} // Rewrite release-(oneway void) release{} // Rewrite autorelease-(id) autorelease{    return self;}

 

? So far, our Singleton mode has been basically created. Let's start our test;

? The code in the main function is as follows:

12345678910111213141516171819 // Test the singleton ModeSingletonClass *single1 = [SingletonClass getInstance];SingletonClass *single2 = [SingletonClass new];SingletonClass *single3 = [[SingletonClass alloc] init];SingletonClass *single4 = [single1 copy];SingletonClass *single5 = [single1 mutableCopy];SingletonClass *single6 = [single1 retain];[single1 release]; [single1 singletonFunction];NSLog(@"single_retainCount = %lu", single1.retainCount); // Output addressNSLog(@"getInstance     single1_P = %p", single1);NSLog(@"new             single2_P = %p", single2);NSLog(@"allo            single3_P = %p", single3);NSLog(@"copy            single4_P = %p", single4);NSLog(@"mutableCopy     single5_P = %p", single5);NSLog(@"retain          single6_P = %p", single6);

 

? ? The running result is as follows:

12345678 16:04:44. 207 memory [20664: 303] Singleton PS: I'm a test method in singleton mode !!2014-08-07 16:04:44.207 Memory[20664:303] single_retainCount = 184467440737095516152014-08-07 16:04:44.207 Memory[20664:303] getInstance     single1_P = 0x1002046902014-08-07 16:04:44.208 Memory[20664:303] newsingle2_P = 0x1002046902014-08-07 16:04:44.208 Memory[20664:303] allo            single3_P = 0x1002046902014-08-07 16:04:44.208 Memory[20664:303] copy            single4_P = 0x1002046902014-08-07 16:04:44.209 Memory[20664:303] mutableCopy     single5_P = 0x1002046902014-08-07 16:04:44.209 Memory[20664:303] retain          single6_P = 0x100204690

? ? ? The address of the singleton remains unchanged.

 

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.