单例设计模式

作者: Coder007 | 来源:发表于2016-12-17 18:19 被阅读28次

    单例设计模式

    • 单例设计模式:让类的对象称为系统中唯一的实例
    • 想要实现单例的类,必须遵守NSCopying和NSMutableCopying协议
    • 重写allocWithZone:方法
    • 重写copyWithZone:方法
    • 重写mutableCopyWithZone:方法
    • 提供访问单例的方法shareSoundTool
    • MRC还需要重写其他的方法
    static SoundTool *_instance = nil;
    + (instancetype)shareSoundTool
    {
        SoundTool *instance = [[self alloc] init];
        return instance;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [[super allocWithZone:zone] init];
        });
        return _instance;
    }
    
    
    - (id)copyWithZone:(NSZone *)zone{
    
        return _instance;
    }
    
    - (id)mutableCopyWithZone:(NSZone *)zone
    {
        return _instance;
    }
    
    // MRC
    #if !__has_feature(objc_arc)
    - (oneway void)release
    {
    }
    
    - (instancetype)retain
    {
        return _instance;
    }
    
    - (NSUInteger)retainCount
    {
        return  MAXFLOAT;
    }
     
    - (instancetype)autorelease
    {
        return self;
    }
    #endif
    

    单例可以定义成宏

    // .h文件中方法声明
    #define interfaceSingleton(name)  +(instancetype)share##name
    
    // .m文件中方法实现
    #if __has_feature(objc_arc)
    // ARC
    #define implementationSingleton(name)  \
    static name *_instance = nil; \
    + (instancetype)share##name \
    { \
        name *instance = [[self alloc] init]; \
        return instance; \
    } \
    + (instancetype)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [[super allocWithZone:zone] init]; \
        }); \
        return _instance; \
    } \
    - (id)copyWithZone:(NSZone *)zone{ \
        return _instance; \
    } \
    - (id)mutableCopyWithZone:(NSZone *)zone \
    { \
        return _instance; \
    }
    #else
    // MRC
    
    #define implementationSingleton(name)  \
    static name *_instance = nil; \
    + (instancetype)share##name \
    { \
        name *instance = [[self alloc] init]; \
        return instance; \
    } \
    + (instancetype)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instance = [[super allocWithZone:zone] init]; \
        }); \
        return _instance; \
    } \
    - (id)copyWithZone:(NSZone *)zone{ \
        return _instance; \
    } \
    - (id)mutableCopyWithZone:(NSZone *)zone \
    { \
        return _instance; \
    } \
    - (oneway void)release \
    { \
    } \
    - (instancetype)retain \
    { \
        return _instance; \
    } \
    - (NSUInteger)retainCount \
    { \
        return  MAXFLOAT; \
    }\
    - (instancetype)autorelease\
    {\
        return self;\
    }
    #endif
    

    相关文章

      网友评论

        本文标题:单例设计模式

        本文链接:https://www.haomeiwen.com/subject/blxamttx.html