This is my strategy so far:
I declared this protocol:
@protocol BGSuperSingletonProtocol <NSObject>
+(id) singleton1;
+(instancetype)singleton;
@end
Then I created a category where all NSObject support that protocol. However, I do not want all NSObject to support that protocol. I just want some NSObject to do. So I will insert this header in .m files rather than .h files
#import "BGSuperSingletonProtocol.h"
@interface NSObject (singleton) <BGSuperSingletonProtocol>
@end
Then I write the implementation of that category
#import "NSObject+singleton.h"
@implementation NSObject (singleton)
static NSMutableDictionary * allTheSingletons;
+(instancetype)singleton
{
return [self singleton1];
}
+(id) singleton1
{
NSString* className = NSStringFromClass([self class]);
if (!allTheSingletons)
{
allTheSingletons = NSMutableDictionary.dictionary;
}
id result = allTheSingletons[className];
//PO1(result);
if (result==nil)
{
result = [[[self class] alloc]init];
allTheSingletons[className]=result;
[result additionalInitialization];
}
return result;
}
-(void) additionalInitialization
{
}