1

I have created a singleton class, And Here is code

static DPGameManager *sharedManager = nil;  
+ (DPGameManager *)sharedManager  
{  
    static dispatch_once_t singletonPredicate;  
    dispatch_once (&_singletonPredicate, ^(){  
             sharedManager = [[DPGameManager alloc]init];  
});  
return sharedManager;  
}

DPGameManager *m1 = [DPGameManager sharedManager];  
DPGameManager *m2 = [DPGameManager alloc]init];  
DPGameManager *m3 = [DPGameManager alloc]init];  

m1, m2, m3 are three different objects.
But we should not be able to create three different object for a singleton class.
How can we achieve this.?
Or is This fine to create different object for a Singleton class.

  • In a good implementation of a Singleton Pattern the init will not create 3 different instance, but should give you the same one, creating a new one only the first time. You need to implement your own init method (and all other init methods) to be sure that the class will have only one instance – Marco Pace Oct 21 '13 at 07:22
  • @MarcoPace Thanks... I got answer of my one question that singleton should create one and only one object. But, can you provide some piece of code which implement own init methods in case of singleton class. – Rajan Kumar Tiwari Oct 21 '13 at 07:30
  • @RajanKumarTiwari I have found [this singleton template](http://blog.mugunthkumar.com/coding/objective-c-singleton-template-for-xcode-4/) by Mugunth Kumar useful at many times. – Amar Oct 21 '13 at 08:01
  • You don't have to explicitly allocate singleton: `+ (DPGameManager *)sharedManager` does that at the first call. You can (and possibly should - to avoid confusion) always refer to is as `[DPGameManager sharedManager]` and you're good to go. – Rok Jarc Oct 22 '13 at 07:06

1 Answers1

-1

First of all: A singleton is a class of which you can create one and only one instance object. This is the usual definition and it is the definition that Apple uses. But, funny thing, in the same documentation the examples contains cases, in which this rule is not fulfilled. (I. e. NSFileManager IIRC.) So in Apple's opinion there is not big difference between shared instances and singletons even there is a difference. (Shared instances are "special instances", singletons are lonesome cowboys.)

So – maybe – it is good enough to adopt this "shared instances are singletons even they are not" antipattern. I wouldn't do that. But probably nobody would even think of +alloc, when he finds a method called +sharedInstance. So – again maybe – it is no big deal.

While it has been very easy to adopt the singleton pattern with manual RC, it became a little bit harder with automatic RC. I think the easiest way is to return sharedInstance in -init… as mentioned in the comments. -init… methods are ownership consuming and ownership transferring. (This is not mentioned in Apple's documentation. You should always read the clang docs for this subjects. They are better by far.) So the old instance delivered by +alloc will be consumed and move away.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • [NSFileManager defaultManager] is a singleton which can be used easily for many purposes. [[NSFileManager alloc] init] is intentionally not a singleton. It can be used when you need to modify the NSFileManager object before using it, which wouldn't work with multiple threads with a singleton. – gnasher729 Mar 04 '14 at 18:42
  • A singleton class has only one instance object by definition. By wiki's definition ("is a design pattern that restricts the instantiation of a class to *one object*."), by Apple's definition ("whereas with a singleton class, there can be only *one instance* of the class per process.") Probably there is nobody on this world, who declares an *instance* to adopt a singleton pattern. Classes adopt the singleton pattern. Please read the documentation. – Amin Negm-Awad Mar 05 '14 at 08:14
  • Well, this is Objective-C, where we take a pragmatic approach. – gnasher729 Apr 14 '14 at 17:41
  • Nothing wrong with doing so. But it is wrong to mix up things and use wrong terms. This is not pragmatic. – Amin Negm-Awad Apr 15 '14 at 05:03