#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