static WeiboMsgManager *weiboInstace = nil;
@implementation WeiboMsgManager
///singleton should be Rigorous
+(WeiboMsgManager*)ShareInstance{
CCLog();
@synchronized(self){
if (weiboInstace == nil) {
weiboInstace = [[self alloc]init];
}
}
return weiboInstace;
}
-(id)init{
CCLog();
self = [super init];
if (self) {
///通常在这里初始化相关工作
}
return self;
}
/// 这个 dealloc 方法永远都不会被调用--因为在程序的生命周期内容,该单例一直都存在。(所以该方法可以不 用实现)
-(void)dealloc
{
[super dealloc];
}
// 通过返回当前的 sharedInstance 实例,就能防止实例化一个新的对象。
+(id)allocWithZone:(NSZone *)zone{
CCLog();
//return [[self ShareInstance] retain];/////!!!!!?????
@synchronized(self){
if (weiboInstace == nil) {
weiboInstace = [super allocWithZone:zone];
return weiboInstace;
}
}
return nil;
}
- (id)copyWithZone:(NSZone *)zone {
////????
return self;
}
-(id)retain{
return self;
}
///
-(NSUInteger)retainCount{
return 1;
}
-(oneway void)release{
}
-(id)autorelease{
return self;
}
本文介绍了一个使用Objective-C编写的微博消息管理器类,该类采用了单例模式实现。文章详细展示了如何通过同步机制确保单例对象的唯一性和线程安全性,并实现了基本的方法覆盖以确保对象的正确管理和内存使用。
1511

被折叠的 条评论
为什么被折叠?



