Creating a singleton instance

Source: Internet
Author: User
Creating a singleton instance

Some classes of the Foundation and appkit frameworks create Singleton objects. in a strict implementation, a singleton is the sole allowable instance of a class in the current process. but you can also have a more flexible Singleton implementation in which a factory method always returns the same instance, but you can allocate and initialize Additional instances. theNsfilemanagerClass fits this latter pattern, whereasUiapplicationFits the former. When you ask for an instanceUiapplication, It passes you a reference to the sole instance, allocating and initializing it if it doesn't yet exist.

A singleton object acts as a kind of control center, directing or coordinating the services of the class. your class shocould generate a singleton instance rather than multiple instances when there is conceptually only one instance (as with, for example,Nsworkspace). You use Singleton instances rather than factory methods or functions when it is conceivable that there might be multiple instances someday.

To create a singleton as the sole allowable instance of a class in the current process, you need to have an implementation similarListing 2-15. This Code does the following:

  • It declares a static instance of your singleton object and initializes itNil.

  • In your class factory method for the class (named something like "sharedinstance" or "sharedmanager"), it generates an instance of the class but only if the static instance isNil.

  • It overridesAllocwithzone:Method To ensure that another instance is not allocated if someone tries to allocate and initialize an instance of your class directly instead of using the class factory method. Instead, it just returns the shared object.

  • It implements the base protocol MethodsCopywithzone:,Release,Retain,Retaincount, AndAutoreleaseTo do the appropriate things to ensure Singleton status. (the last four of these methods apply to memory-managed code, not to garbage-collected code .)

Listing 2-15Strict implementation of a singleton

 1   Static Mygizmoclass * sharedgizmomanager = Nil;  2    3 + (Mygizmoclass * ) Sharedmanager  4   {  5       If (Sharedgizmomanager = Nil ){  6 Sharedgizmomanager =[[Super allocwithzone: NULL] init];  7   }  8       Return  Sharedgizmomanager;  9   }  10    11 + ( ID ) Allocwithzone :( nszone * ) Zone  12   {  13       Return [[Self sharedmanager] retain];  14   }  15    16 -( ID ) Copywithzone :( nszone * ) Zone  17   {  18       Return  Self;  19   }  20    21 -(ID  ) Retain  22   {  23       Return  Self;  24   }  25    26 - (Nsuinteger) retaincount  27   {  28       Return Nsuintegermax; // Denotes an object that cannot be released  29   }  30    31 -( Void  ) Release  32   {  33       //  Do nothing  34   }  35    36 -( ID ) Autorelease  37   {  38       Return  Self;  39 }

If you want a singleton instance (created and controlled by the class factory method) but also have the ability to create other instances as needed through allocation and initialization, do not overrideAllocwithzone:And the other methods following it as shown inListing 2-15.

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.