A singleton in iOS

Source: Internet
Author: User

#import "Singleton.h"@implementationSingletonStaticSingleton *singleton =Nil;+ (Singleton *) sharesingleton{@synchronized (self) {if(!Singleton) {Singleton=[[Self alloc]init]; }        returnSingleton; }}+ (ID) alloc{@synchronized (self) {singleton=[Super alloc]; returnSingleton; }}@end

The singleton is also a design pattern, so far there are three types of design Patterns: Observer mode, proxy mode, and singleton mode.

By writing this single example, I understand:

1. A static method instantiates a class is actually an instantiation of itself in this class, which means that only one class instance exists throughout the software and is used globally.

The 2.static keyword is initialized at compile time and then is no longer initialized when it is run to the static code, scoped only in the current. m file, as long as. M does not destroy, and static variables persist and save their variable values.

A singleton in iOS

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.