iOS design mode-single case
Schematic diagram
Description
1. Singleton mode is used by everyone, strict singleton mode is seldom used
2. Strict singleton mode means that the object cannot be generated by the normal Alloc init method, and the derived subclass cannot produce the object, but only through the singleton method to get to the object
Source
Https://github.com/YouXianMing/SingletonPattern
// // Singleton.h// singletonpattern//// Created by Youxianming on 15/8/6. // Copyright (c) 2015 youxianming. All rights reserved. // #import <Foundation/Foundation.h>@interface singleton:nsobject+ (Singleton *) sharedinstance; @end
////singleton.m//Singletonpattern////Created by youxianming on 15/8/6.//Copyright (c) 2015 youxianming. All rights reserved.//#import "Singleton.h"#defineStr_singleton @ "Str_singleton"StaticSingleton *_sharedsingleton =Nil;@implementationSingleton+ (Singleton *) sharedinstance {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_sharedsingleton= (Singleton *) Str_singleton; _sharedsingleton=[[Singleton alloc] init]; }); return_sharedsingleton;}-(instancetype) init {nsstring*string= (NSString *) _sharedsingleton; if([stringIskindofclass:[nsstringclass]] && [stringIsequaltostring:str_singleton]) { Self=[Super Init]; if(self) {}returnSelf ; } Else { returnNil; }}@end
Details
Tips to ensure that instances can only be obtained from the Shareinstance method
iOS design mode-single case