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"#define Str_singleton @ "Str_singleton"static Singleton *_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 *< Span style= "color: #000000;" >) _sharedsingleton; if ([string iskindofclass:[nsstring class]] && [string isequaltostring:str_ SINGLETON] {self = [super Init]; ifreturn< Span style= "color: #000000;" > self; } else {return nil; }} @end
Details
Tips to ensure that instances can only be obtained from the Shareinstance method
iOS design mode-single case