Objective-C - iOS - Singleton explanation -


this code of singleton

+ (aldata *)sharedinstance {      static aldata *_shared;     if(!_shared) {         static dispatch_once_t oncepredicate;         dispatch_once(&oncepredicate, ^ {             _shared = [[super allocwithzone:nil] init];         });     }      return _shared; }  + (id)allocwithzone:(nszone *)zone { return [self sharedinstance]; } - (id)copywithzone:(nszone *)zone { return self; } #if (!__has_feature(objc_arc)) - (id)retain { return self; } - (unsigned)retaincount { return uint_max; } - (void)release {} - (id)autorelease { return self; } #endif 

now, singletons have seen being called in way :

[[singleton sharedinstance] instancemethod]; 

but want call in way :

[singleton classmethod]; 

to when create method :

+ (bool)decide:(bool)var {     [self sharedinstance];     if (var)         return no;     else         return yes; } 

instead, if want proceed in first way have declare method in way :

- (bool)decide:(bool)var {     if (var)         return no;     else         return yes; } 

and when call have write :

[[singleton sharedinstance] decide:yes]; 

my question : difference between these 2 approaches? both in terms of performance, both @ level of design pattern. know because think aesthetically better method second, 1 doesn't call sharedinstance.

i hope question clear

the main difference between singleton , class bunch of class methods singleton can preserve kind of state. example, array of data or boolean flags. calling sharedinstance, access 1 , instance of class, being kept alive(and state of data preserved there). can add class methods singleton class , work, escape singleton pattern.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -