IOs Singleton mode definition, implementation, step

Source: Internet
Author: User

a singleton pattern is a class that has at most one instance object in the system and accesses the instance object through a global portal.

For shared resources, it is unnecessary to implement multiple objects encapsulation, not only to occupy system resources, but also to cause conflicts, so a singleton mode is generally used.

#import "DSSingletonObject.h" @implementation dssingletonobject+ (Dssingletonobject *) defaultobject {    static Dssingletonobject *defaultobjectinstance = nil;        if (!defaultobjectinstance)        defaultobjectinstance = [[Super Allocwithzone:null] init];        return defaultobjectinstance;} + (ID) allocwithzone: (struct _nszone *) zone {    return [self defaultobject];}
@end

  

#import <Foundation/Foundation.h> @interface dssingletonobject:nsobject//A + (instancetype) defaultobject;// Two: Thread safety, after iOS4 commonly used this + (Dssingletonobject *) Sharedobject; @end

  

int main (int argc, const char * argv[]) {    Dssingletonobject *obj = [Dssingletonobject defaultobject];    NSLog (@ "%p", obj);        Dssingletonobject *obj2 = [[Dssingletonobject alloc] init];    NSLog (@ "%p", obj2);        Dssingletonobject *obj3 = [[Dssingletonobject alloc] init];    NSLog (@ "%p", obj3);                return 0;}

  

IOs Singleton mode definition, implementation, step

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.