单例设计模式的实现,以及为什么使用单例模式
static File *instance = nil;
@implementation File
//得到单例的方法
+ (id)shareInstance{
@synchronized (self) {
if (instance == nil) {
instance = [[File alloc] init];
}
}
return instance;
}
//复写allocWithZone,copyWithZone,retain,release,autorrelease方法,目的是限制这个类只能创建一个对象
为什么使用单例对象
1.单例设计是用来限制一个类只能创建一个对象,那么此对象中的属性可以存储全局共享的数据,所有的类都可以访问,设置此单例对象中的属性数据
2.如果一个类创建的时候非常消耗性能,那么此类可以设置为单例节约性能,如果能满足需求的话,