Lud*_*uda 2 singleton design-patterns ios
我有几个应该从某个A类继承的类.
他们每个人都应该是一个单身人士.
这可以实现吗?
Mik*_*nov 10
Singleton-pattern的这种实现允许继承:
+ (instancetype)sharedInstance {
    static dispatch_once_t once;
    static NSMutableDictionary *sharedInstances;
    dispatch_once(&once, ^{ /* This code fires only once */
        // Creating of the container for shared instances for different classes
        sharedInstances = [NSMutableDictionary new];
    });
    id sharedInstance;
    @synchronized(self) { /* Critical section for Singleton-behavior */
        // Getting of the shared instance for exact class
        sharedInstance = sharedInstances[NSStringFromClass(self)];
        if (!sharedInstance) {
            // Creating of the shared instance if it's not created yet
            sharedInstance = [self new];
            sharedInstances[NSStringFromClass(self)] = sharedInstance;
        }
    }
    return sharedInstance;
}