iOS单例的应用

单例设计模式的基本步骤:

  • 声明一个单件对象的静态实例,并初始化为nil
  • 创建一个类的类工厂方法,当且仅当这个类的实例为nil时生成一个该类的实例
  • 实现 NScopying 协议, 覆盖 allocWithZone: 方法,确保用户在直接分配和初始化对象时,不会产 生另一个对象。
  • 覆盖 releaseautoreleaseretainretainCount 方法, 以此确保单例的状态。
  • 在多线程的环境中,注意使用 @synchronized 关键字或 GCD,确保静态实例被正确的创建和初始化。

MRC写法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法内部会调用allocWithZone
     * zone:系统分配给app的内存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
    
    - (oneway void)release{
        
    }
    
    - (instancetype)retain{
        return self;
    }
    
    - (NSUInteger)retainCount{
        return 1;
    }
    
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

ARC写法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法内部会调用allocWithZone
     * zone:系统分配给app的内存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
        
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

若需要创建多个单例可将其写成宏

使用以下语法来区分 ARCMRC

#if __has_feature(objc_arc) //ARC
#else //MRC
#endif

代码如下:

    //
    //  Singleton.h
    //  LantaiyuanBus
    //
    //  Created by lantaiyuan on 16/9/7.
    //  Copyright © 2016年 youmy. All rights reserved.
    //
    
    // .h文件的实现
    #define SingletonH(methodName) + (instancetype)shared##methodName;
    
    // .m文件的实现
    #if __has_feature(objc_arc) // 是ARC
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #else // 不是ARC
    
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    \
    - (oneway void)release \
    { \
    \
    } \
    \
    - (id)retain \
    { \
    return self; \
    } \
    \
    - (NSUInteger)retainCount \
    { \
    return 1; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #endif

用法:

.h 文件

#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface VideoTool : NSObject
SingletonH(VideoTool)
@end

.m 文件

#import "VideoTool.h"
@implementation VideoTool
SingletonM(VideoTool)
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容