Objective-c design Pattern--Singleton Singleton (object creation)

Source: Internet
Author: User

Single case

and other languages of the singleton production is not much, can be said to be the simplest of a design pattern. But there are a few points to note that a singleton is just one instance of a class.

So we're going to try to stop this class from generating other instances, and the constructor is written as private in the general language. But the functions in OC do not have qualifiers, so we need some tips to block this.

Application Scenarios

A class can have only one instance, and it must be accessed from a well-known access point, such as a factory method.

This unique instance can only be extended by subclasses of the class, and the extended object does not break the client code.

Attention

The instance variable for a singleton in 1.OC is defined in the. m file

A single example in 2.OC requires overloading Allocwithzone: and Copywithzone: Methods to prevent the creation of other instances.

3. Singleton creation be aware of thread safety, or multiple instances may occur.

Note the problem will be explained in the demo

Demo

First of all, let's look at one of the most conventional, non-rigorous single-instance implementations:

@implementation Singleton Static Singleton *sharedinstance; -(Singleton *) sharedinstance{    if(sharedinstance)    {        new];    }     return sharedinstance;} @end

This may seem like a single object, but it can be said to be a variant of the single case. You can't say this is a singleton, because we can easily create objects in other ways.

So we're going to have to modify Allocwithzone: and Copywithzone: Methods (Alloc and copy methods actually call both methods)

-(ID) Copywithzone: (Nszone *) zone{    returnclass] sharedinstance];} + (ID) Allocwithzone: (struct _nszone *) zone{    return  [self Sharedinstance];}

However, there is another problem, in the Sharedinstance method we actually call the Allocwithzone: (New Call Alloc), but its alloc is rewritten by us, this will be an error. So we need to modify the Sharedinstance method

+ (Singleton *) sharedinstance{    if(sharedinstance)    {        = [[Super Allocwithzone:null] init];    }     return sharedinstance;}

This allows for a smooth return of the singleton, and it is not possible to produce an instance object in any other way.

It looks perfect. There will be problems actually, because it is now non-thread-safe and there may be instances where more than one instance is created at the same time, so modify the following

+(instancetype) sharedinstance{    dispatch_once_t once;    Dispatch_once (&once, ^{        = [[Super Allocwithzone:null] init];    });     return sharedinstance;}

The client code is as follows:

        Singleton *singleton = [Singleton sharedinstance];         *singleton2 = [[Singleton alloc] init];         *singleton3 = [singleton copy];         [Singleton print];        [Singleton2 print];        [Singleton3 print];

Output Result:

 -- -- +  +:Ten:32.797singleton[42537:10347987]0x7fff5fbff7a8 -- -- +  +:Ten:32.798singleton[42537:10347987]0x7fff5fbff7a8 -- -- +  +:Ten:32.798singleton[42537:10347987]0x7fff5fbff7a8

You can see that the memory is pointing to the same address.

Objective-c design Pattern--Singleton Singleton (object creation)

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.