oc 单例模式(ARC和MRC)封装宏

本文详细介绍了如何在ARC和MRC环境下使用Objective-C实现单例模式,包括重写allocWithZone方法、提供类方法及实现copy和mutableCopy方法。此外,还提供了单例模式的宏封装,便于快速应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ARC环境下实现单例模式


@interface Test:NSObject<NSCopying,NSMutableCopying>
+(instancetype)default;
@end;

@implementation Test
static Test *instance;
//1.重写allocWithZone
+(instancetype) allocWithZone:(struct _NSZone *)zone{
	//方法一 加互斥锁 解决多线程访问安全问题
	/*@synchronized(self){
		if(_instance==nil){
			_instance = [super allocWithZone:zone];
		}
	}*/
	//方法二  该方法本身就是线程安全的
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken,^{
		_instance = [super allocWithZone:zone];
	});
	return _instance;
}

//2.提供类方法
+(instancetype)default{
	return [[self alloc] init];
}

//3.copy
//调用方法  Test t2 = [t1 copy] | [t1 mutableCopy];
-(id) copyWithZone:(NSZone *)zone{
	return _instance;
}

-(id) mutableCopyWithZone:(NSZone *)zone{
	return _instance;
}
@end

MRC环境下实现单例模式

MRC环境下的单例模式大致与ARC环境下相同,只不过要重写三个方法,在这里我们为了让这段代码即适用于ARC也适用于MRC,我们在代码中加入条件判断#if。


@interface Test:NSObject<NSCopying,NSMutableCopying>
+(instancetype)default;
@end;

@implementation Test
static Test *instance;
//1.重写allocWithZone
+(instancetype) allocWithZone:(struct _NSZone *)zone{
	//方法一 加互斥锁 解决多线程访问安全问题
	/*@synchronized(self){
		if(_instance==nil){
			_instance = [super allocWithZone:zone];
		}
	}*/
	//方法二  该方法本身就是线程安全的
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken,^{
		_instance = [super allocWithZone:zone];
	});
	return _instance;
}

//2.提供类方法
+(instancetype)default{
	return [[self alloc] init];
}

//3.copy
//调用方法  Test t2 = [t1 copy] | [t1 mutableCopy];
-(id) copyWithZone:(NSZone *)zone{
	return _instance;
}

-(id) mutableCopyWithZone:(NSZone *)zone{
	return _instance;
}

#if __has_feature(objc_arc)
//ARC 不做操作
#else
//MRC
-(oneway void)release{
}

-(instancetype)retain{
	return _instance;
}
//约定俗成 当实现单例后返回最大值
-(NSUInteger)retainCount{
	return MAXFLOAT;
}
#endif

@end

抽取代码,封装单例(单例宏)Single.h


为了使默认构造方法能够变成default+类名的形式,所以在使用宏时我们将类名作为参数传给单例宏。

#if __has_feature(objc_arc)
//ARC
#define SingleH(name) +(instancetype) default##name;
#define SingleM(name) static id _instance;\
+(instancetype) allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken,^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)default##name{\
return [[self alloc] init];\
}\
\
-(id) copyWithZone:(NSZone *)zone{\
return _instance;\
}\
\
-(id) mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}
#else
//MRC
#define SingleH(name) +(instancetype) default##name;
#define SingleM(name) static id _instance;\
+(instancetype) allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken,^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)default##name{\
return [[self alloc] init];\
}\
\
-(id) copyWithZone:(NSZone *)zone{\
return _instance;\
}\
\
-(id) mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}\
-(oneway void)release{\
}\
\
-(instancetype)retain{\
	return _instance;\
}\
\
-(NSUInteger)retainCount{\
	return MAXFLOAT;\
}
#endif

使用封装好的单例宏


#import "Single.h"
@interface Test:NSObject<NSCopying,NSMutableCopying>
SingleH(Test)
@end

@implementation Test
SingleM(Test)
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值