Re: iOS: Using AppDelegate as an app-wide singleton
Re: iOS: Using AppDelegate as an app-wide singleton
- Subject: Re: iOS: Using AppDelegate as an app-wide singleton
- From: Luther Baker <email@hidden>
- Date: Tue, 01 Dec 2015 23:24:54 -0600
The conversation here is pretty loose ... and so everyone might be right in
what they are intending to convey... ;-) but I thought I'd just put in a
vote to stop using the term "Singleton" for this access pattern. It isn't a
Singleton (unless there is historical signficance that grandfathers this
incorrect term in) and I think that using the term loosely in this case
actually weakens the power of a pattern vocabulary a bit.
Unless you're doing something we can't see (which is possible), it is
unlikely you are actually creating a Singleton. It is more likely that you
are creating class methods that in some/many/most/all cases - are accessing
private static instances. Similar in form to ...
[NSUserDefaults standardUserDefaults]
[NSNotificationCenter defaultCenter]
Neither NSApplication, NSUserDefaults nor NSNotificationCenter technically
adhere to the Singleton pattern -- although everyone labels this technique
as such.
https://en.wikipedia.org/wiki/Singleton_pattern
Alex, the API uses lots of different words to grab a-hold of the so-called
"standard", "default" or "shared" static instance. Unfortunately, I'm not
aware of any docs that specify when to use which adjective or if any one
particular adjective should be used when creating a real Singleton ...
since none of these are!
That said, the names all seem contextually valid ... and yet completely
ignore the idea of reflecting their identical access pattern.
Maybe someone can chime in with some historical context. There are better
ways to implement/approximate real Singletons ... they just aren't obvious
(and may not be necessary) .... aaaaaand, this line of conversation is
probably getting dry by now ;-) Happy to hear someone chime in on this
perspective!
Cheers,
-Luther
On Tue, Dec 1, 2015 at 9:15 PM, Alex Zavatone <email@hidden> wrote:
> What does Apple do on this?
>
> I think their standard is to use shared in the name.
>
>
> On Dec 1, 2015, at 7:33 PM, Quincey Morris <
> email@hidden> wrote:
>
> > On Dec 1, 2015, at 16:20 , Carl Hoefs <email@hidden>
> wrote:
> >
> >> The following seems to be working out for me.
> >>
> >> #import "AppCommon.h"
> >> @implementation AppCommon
> >> + (AppCommon *)shared
> >> {
> >> static AppCommon *shared = nil;
> >> static dispatch_once_t token;
> >> dispatch_once(&token, ^{
> >> shared = [[self alloc] init];
> >> });
> >> return shared;
> >> }
> >
> > Looks functionally perfect.
> >
> > PSA #1: I would encourage you avoid naming the method ‘shared’, indeed
> to avoid naming anything with a brief name that doesn’t say what it is.
> Even the argument that it’s easier to type isn’t good any more, since Xcode
> is going to autocomplete almost every name for you.
> >
> > PSA #2: I can’t help mentioning that you can write all of this in Swift
> as so:
> >
> > class AppCommon {
> > static let shared = AppCommon ()
> > }
> >
> > Sometimes, Swift really is Obj-C-but-better. ;)
> >
> > _______________________________________________
> >
> > Cocoa-dev mailing list (email@hidden)
> >
> > Please do not post admin requests or moderator comments to the list.
> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden