iOS singleton mode (Singleton)

Source: Internet
Author: User

The singleton pattern means that there is only one instance. The singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole. This class is called a singleton class.

1. Key points of the singleton mode:

  It is obvious that there are three main points of the singleton pattern; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.

2. Advantages of Singleton mode:

1. Instance control: Singleton prevents other objects from instantiating copies of their own Singleton objects, ensuring that all objects have access to unique instances. 2. Flexibility: Because classes control the instantiation process, classes can be more flexible in modifying the instantiation process Singleton mode in iOS
 static  singletonmanage* sharedinstance = Nil;  + (singletonmanage *) instance { static  Span style= "color: #000000;"    > dispatch_once_t Oncetoken; Dispatch_once ( &oncetoken, ^{sharedinstance  = [[Super Alloc]init];        });  return   sharedinstance;}  + (id ) Allocwithzone: (Nszone *) zone{NSLog (  @ " you need to use instance class method instead!   "    );  return   nil;}  

Creating a Singleton Instance

Some classes of the Foundation and AppKit frameworks create singleton objects. In a strict implementation, a singleton are the sole allowable instance of a class in the current process. But can also has a more flexible singleton implementation in which a factory method always returns the same instance, But you can allocate and initialize additional instances. NSFileManagerthe class fits this latter pattern, whereas the UIApplication fits the former. When your ask for an instance UIApplication of, it passes you a reference to the sole instance, allocating and initializing it if I T doesn ' t yet exist.

A singleton object acts as a kind of control center, directing or coordinating the services of the class. Your class should generate a singleton instance rather than multiple instances when there was conceptually only one instanc E (as with, for example, NSWorkspace ). Singleton instances rather than factory methods or functions when it's conceivable that there might be multiple I Nstances someday.

To create a singleton as the sole allowable instance of a class in the current process and you need to a implementation Similar to Listing 2-15. This code does the following:

  • It declares a static instance of your singleton object and initializes it to nil .

  • In your class factory method for the class (named something as "Sharedinstance" or "Sharedmanager"), it generates an INS Tance of the class but only if the static instance is nil .

  • It overrides the allocWithZone: method to ensure that another instance are 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 methods,,,, and to does the copyWithZone: release retain retainCount autorelease appropriate things to ensure SINGL Eton status. (The last four of these methods apply to Memory-managed code, and not to garbage-collected code.)

Listing 2-15 Strict Implementation of a singleton

static Mygizmoclass *sharedgizmomanager = nil;
+ (mygizmoclass*) Sharedmanager
{
    if (Sharedgizmomanager = = nil) {
        Sharedgizmomanager = [[Super Allocwithzone:null] init];
    }
    return sharedgizmomanager;
}
+ (ID) Allocwithzone: (Nszone *) zone
{
    return [[Self sharedmanager] retain];
}
-(ID) Copywithzone: (Nszone *) zone
{
    return self;
}
-(ID) retain
{
    return self;
}
-(Nsuinteger) Retaincount
{
    return Nsuintegermax;  Denotes an object, cannot be released
}
-(void) Release
{
    Do nothing
}
-(ID) autorelease
{
    return self;
}

If you want a singleton instance (created and controlled by the class factory method) but also has the ability to create Other instances as needed through allocation and initialization, does not override and the other allocWithZone: methods following it As shown in Listing 2-15.

Https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html

iOS singleton mode (Singleton)

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.