0

What would be equivalent Objective C for this:

template<class T>
class Singleton 
{ 
public: 
    static T * instance() 
    { 
        static T t; 
        return &t; 
    }
private: 
    Singleton(); 
    ~Singleton(); 
};

While calling:

friend class Singletone<MyManagerClass>;

halfer
  • 19,824
  • 17
  • 99
  • 186
iPhoneDev
  • 2,995
  • 4
  • 33
  • 44

4 Answers4

2

Objective C singleton examples

Community
  • 1
  • 1
wadesworld
  • 13,535
  • 14
  • 60
  • 93
  • Above C++ code is generic for creating Singleton object for any class. Which is not in your case. I am looking for Objective c class which can return singleton object for a given class. thanks – iPhoneDev Aug 13 '10 at 07:23
2

Objective-C does not provide templates and templates do not work with Objective-C classes when compiled as Objective-C++. It is not always possible to have a 1-to-1 conversion between two programming languages. For example, Objective-C does not provide compile-time enforced private methods at all, so there is no way to exactly implement what you want.

The same goes the other way, Objective-C has features that are not available in C++, such as the @encode() directive.

Perhaps related to this topic: it is important to understand that design patterns that work well with C++ do not necessarily work well—or at all—with Objective-C.

What is the goal of your design of being able to singletonise any class? Perhaps there is a more established Objective-C way to acheive what you want. There may also be a way to implement something similar using preprocessor macros, but you don't want to go down that path.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Thanks, so what should I write in objective c to acheive this.. please explore a bit – iPhoneDev Aug 13 '10 at 09:40
  • I got the way to crate a generic singleton calss in objective c, some thing like this ..#define SINGLETON_FOR_CLASS(classname) \ \ static classname *shared##classname = nil; – iPhoneDev Jan 04 '11 at 19:17
  • @iPhoneDev: What is your ultimate goal though? How many singletons are you planning on having? Can they not simply be properties of your application delegate? – dreamlax Jan 04 '11 at 19:54
  • All my business layer is static, its always better to write a generic method which take class name as a parameter and return static object for that. This what I have done. – iPhoneDev Feb 07 '11 at 09:06
0

Oh thats Pretty easy!

@interface YourClass : NSObject {}
+ (YourClass *)singleton;
@end

static YourClass *singletonVar = nil;
@implementation YourClass
+ (YourClass *)singleton
{
  if( ! singletonVar ) {
    singletonVar = [[self alloc] init];
  }
  return singletonVar;
}
@end

Now you can call [[YourClass singleton] doSomething] anywhere! :D Just like you do [[NSUserDefaults standUserDefaults] valueForKey:@"Burp"] and such, do note that this ads overhead to your app, so when you use the singleton more than once in a scope consider storing it in a pointer for performance.

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
  • Above C++ code is generic for creating Singleton object for any class. Which is not in your case. I am looking for Objective c class which can return singleton object for a given class. thanks – iPhoneDev Aug 13 '10 at 07:20
0
#define SINGLETON_FOR_CLASS(classname)                          \
                                                                \
static classname *shared##classname = nil;                      \
                                                                \
+ (classname *)instance {                                       \
    @synchronized(self) {                                       \
        if (shared##classname == nil) {                         \
            [[self alloc] init];                                \
        }                                                       \
    }                                                           \
                                                                \
    return shared##classname;                                   \
}                                                               \
                                                                \
+ (id)allocWithZone:(NSZone *)zone {                            \
                                                                \
    @synchronized(self) {                                       \
        if (shared##classname == nil) {                         \
            shared##classname = [super allocWithZone:zone];     \
            return shared##classname;                           \
        }                                                       \
    }                                                           \
                                                                \
    return nil;                                                 \
}                                                               \
                                                                \
- (id)copyWithZone:(NSZone *)zone {                             \
                                                                \
    return self;                                                \
}                                                               \
                                                                \
- (id)retain {                                                  \
                                                                \
    return self;                                                \
}                                                               \
                                                                \
- (NSUInteger)retainCount {                                     \
                                                                \
    return UINT_MAX;                                            \
}                                                               \
                                                                \
- (void)release {                                               \
                                                                \
}                                                               \
                                                                \
- (id)autorelease {                                             \
                                                                \
    return self;                                                \
}                                                               \
benzado
  • 82,288
  • 22
  • 110
  • 138
iPhoneDev
  • 2,995
  • 4
  • 33
  • 44