@interface TaskCmdCtrler : NSObject
+(TaskCmdCtrler*) shareInstance;
@implementation TaskCmdCtrler
+(TaskCmdCtrler*) shareInstance
{
if (s_instance)
return s_instance;
@synchronized(self)
{
if (nil==s_instance)
{
s_instance = [[super allocWithZone:nil] init];
}
}
returns_instance;
}
+(id) allocWithZone:(NSZone *)zone
{
if ([TaskCmdCtrler class]==self)
return [self shareInstance];
else
return [super allocWithZone:zone];
}
注意:需要覆盖
+(id) allocWithZone:(NSZone *)zone
函数;防止发生多例;
另外,+(id) allocWithZone:(NSZone *)zone 的 if else写法,防止在单例发生继承的情况下,返回的单例还是父类单例。