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