iOS开发-面试

单例模式的理解

2018-04-18  本文已影响52人  目前运行时
// 1.在该类中定义一个静态的全局变量,防止被外部用extren访问
static id _instance;
/**

 static : 修饰变量

 1> 修饰全局变量

 * 全局变量的作用域仅限于当前文件内部,其他文件不能用extren关键字访问

 2> 修饰局部变量 : 

 * 局部变量的生命周期 跟 全局变量 类似

 * 但是不能改变作用域

 * 能保证局部变量永远只初始化1次,在程序运行过程中,永远只有1分内存

 */

 
/**
 * 2.重写它这个类的llocWithZone:方法,确保只为你这个类分配一次内存地址
 *  alloc方法内部会调用这个方法
 */
+ (id)allocWithZone:(struct _NSZone *)zone
{
    if (_instance == nil) { // 防止频繁加锁
        @synchronized(self) { // 加锁,避免在多线程中出错而创建多个对象
            if (_instance == nil) { // 防止创建多次
                _instance = [super allocWithZone:zone]; 
            }
        }
    }
    return _instance;
}

// 3.提供一个shared方法让外界调用这个单例(一般单例都会提供这个方法),确保只init一次
+ (instancetype)sharedMusicTool
{
    if (_instance == nil) { // 防止频繁加锁
        @synchronized(self) {
            if (_instance == nil) { // 防止创建多次
                _instance = [[self alloc] init];
            }
        }
    }
    return _instance;
}

// 4.重写copyWithZone:方法,避免使用copy时创建多个对象
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

解释:其实要将写好点 我们还需要实现他的mutablecopy 方法,为什么枷锁 就是防止多线程出错从而不是创建不是一次

// 1.在该类中定义一个静态的全局变量,防止被外部用extren访问
static id _instance;

/**
 * 2.重写它这个类的llocWithZone:方法,这里不用加锁,因为程序刚启动,线程还没加载,不会出现线程不安全的问题
 */
+ (id)allocWithZone:(struct _NSZone *)zone
{
            if (_instance == nil) { // 防止创建多次
                _instance = [super allocWithZone:zone];
            }
    return _instance;
}

// 3.提供一个shared方法让外界调用这个单例(一般单例都会提供这个方法)
+ (instancetype)sharedMusicTool
{
    return _instance;
}

// 4.重写copyWithZone:方法,避免使用copy时创建多个对象
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

// 5.重写load这个类方法,在里面alloc它
// 这个方法在程序启动,加载类的时候会调用一次
+ (void)load
{
  _instance = [[self alloc] init];      
}

解释:这里不用枷锁 没事的 因为程序刚启动线程还没有加载 所用不用枷锁 为了更加安全 枷锁更好了。

// 1.
static id _instance;

/**
 * 2.用GCD的dispatch_once方法重写
 */
+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         _instance = [super allocWithZone:zone];
    });
   return _instance;
}

// 3.
+ (instancetype)sharedMusicTool
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         _instance = [[self alloc] init];
    });
    return _instance;
}

// 4.
- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}
// 在.h文件使用的宏定义
#define HJSingletonH(name) + (instancetype)shared##name;

#if __has_feature(objc_arc)
// 当前的编译器环境是ARC
// 在.m文件使用的宏定义
#define HJSingletonM(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
}

#else
// 当前的编译器环境是MRC
// 在.m文件使用的宏定义
#define HJSingletonM(name) \
static id _instace; \
 \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [super allocWithZone:zone]; \
    }); \
    return _instace; \
} \
 \
+ (instancetype)shared##name \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [[self alloc] init]; \
    }); \
    return _instace; \
} \
 \
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instace; \
} \
- (oneway void)release {} \
- (id)retain {return self;} \
- (NSUInteger)retainCount {return 1;} \
- (id)autorelease {return self;}
#endif

解释:这个带ARC和MRC的

上一篇 下一篇

猜你喜欢

热点阅读