Hacker News new | ask | show | jobs
by klibertp 5040 days ago
Singleton method? I'm admittedly not a DP geek, could you please tell me what it is used for and point to example reification(s)?
2 comments

Just yesterday used a singleton in Objective-C due to a webservice that worked with a challenge-response system. Whenever I perform a request, I receive a code. The code is required in the next request to retrieve the data. The issue I was having, was sending lots of requests in different threads; this could cause a code retrieved in a previous request to become invalid, making an older request for data fail.

I figured that by creating a singleton class responsible for executing requests sequentially, this problem would be fixed (and it did). This singleton class internally uses a FIFO-queue to execute all requests sequentially. I think it was an appropriate solution, especially since this singleton class is used internally, not exposed through interfaces to other classes interacting with my 'framework'.

I have nothing against singletons! (I'm referring to your somewhat defensive "I think it was an appropriate solution, especially since this singleton class is used internally,...")

I was just curious about what 'singleton method' meant, as opposed to 'singleton object'. Sadly, it seems that it would be very hard to implement something similar in Python (my main language at the moment) without creating a metaclass. But I certainly saw this pattern in Smalltalk before, just didn't know how it's called.

I use these in objective C, I usually create a data object and stick the networking function in it as well. Some use core data, but I haven't found a need for it yet.

static MySingleton *sharedSingleton;

+ (void)initialize { static BOOL initialized = NO; if(!initialized) { initialized = YES; sharedSingleton = [[MySingleton alloc] init]; } }

+1 Informative. Thanks! :)