美文网首页
单例设计模式(ARC和非ARC)

单例设计模式(ARC和非ARC)

作者: Hevin_Chen | 来源:发表于2016-09-18 21:21 被阅读35次

    单例设计模式:

    -    1.就是在整个项目中,这个类的对象只能被初始化一次。拥有唯一的一个实例

    -    2.这个唯一的实例只能通过子类进行拓展,而且拓展的对象不会破坏原来的代码

    它的这种特性,可以广泛的应用于某些需要全局共享的资源中,比如管理类,引擎类,也可以使用单例来实现传值。

    一般的工具类使用单例模式,工具类只有一把就可以了。

    比如:播放音乐的工具类,就可以定义成单例模式,假如有10首歌一次性加载完就可以了,不需要重复加载。

    加载音乐一个耗时的操作,我们在使用播放音乐的软件时,在不同的界面进行跳转时,仍然可以正常播放,而没有因为控制器的销毁

    和新控制器的创建去创建一个新的播放音乐的工具类。

    接来下让用GCD的方式来实现ARC和非ARC的单例设计模式

    static id _instance;

    + (instancetype) sharedDataTool

    {

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    _instance = [[self alloc] init];

    });

    return _instance;

    }

    //使用alloc时

    +(id) allocWithZone:(struct _NSZone *)zone

    {

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    _instance = [[self alloc] init];

    });

    return _instance;

    }

    //使用copy时

    + (id) copyWithZone:(NSZone*) zone{

    return _instance;

    }

    //非ARC情况下只需添加以下代码即可

    //非ARC情况下

    - (oneway void)release{

    }

    - (instancetype)retain{

    return  _instance;

    }

    - (instancetype)autorelease{

    return _instance;

    }

    - (NSUInteger)retainCount{

    return 1;

    }

    ##宏的定义

    // .h文件的实现

    # define Singleton(methodName) + (instancetype)shared##methodName;

    // .m 文件的实现

    ##if __has_feature(objc_arc) // ARC情况下

    #define SingletonM(methodName) \

    static id _instance nil; \

    \

    +(id) allocWithZone:(struct _NSZone *)zone \

    { \

    if (_instance == nil){ \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [super allocWithZone:zone]; \

    }); \

    } \

    return _instance; \

    } \

    \

    - (id)init \

    { \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [super init]; \

    }); \

    return _instance; \

    } \

    +(instancetype) shared##methodName \

    { \

    return [[self alloc]init]; \

    } \

    + (id) copyWithZone:(NSZone*) zone{ \

    \

    return _instance; \

    } \

    + (id)mutableCopyWithZone:(struct _NSZone *)zone \

    { \

    return _instace; \

    }

    //非ARC情况下只需添加以下代码即可

    #else

    #define SingletonM(methodName) \

    static id _instance nil; \

    \

    +(id) allocWithZone:(struct _NSZone *)zone \

    { \

    if (_instance == nil){ \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [super allocWithZone:zone]; \

    }); \

    } \

    return _instance; \

    } \

    \

    - (id)init \

    { \

    static dispatch_once_t onceToken; \

    dispatch_once(&onceToken, ^{ \

    _instance = [super init]; \

    }); \

    return _instance; \

    } \

    +(instancetype) shared##methodName \

    { \

    return [[self alloc]init]; \

    } \

    - (oneway void)release \

    { \

    \

    } \

    - (id)return \

    { \

    return self; \

    } \

    \

    - (NSUInteger)retainCount \

    { \

    return 1; \

    }

    + (id) copyWithZone:(NSZone*) zone{ \

    \

    return _instance; \

    } \

    + (id)mutableCopyWithZone:(struct _NSZone *)zone \

    { \

    return _instace; \

    }

    #endif

    相关文章

      网友评论

          本文标题:单例设计模式(ARC和非ARC)

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