美文网首页
iOS中的单例设计模式

iOS中的单例设计模式

作者: WakeMeUP1 | 来源:发表于2015-09-12 00:09 被阅读367次

什么是单例模式?

单例模式是一种常用的软件设计模式。可以保证通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。

为什么要使用单例模式?

如果我们希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

怎么使用单例模式?

单例模式可以分为饿汉式懒汉式,在iOS中我们经常使用的是懒汉式,下面我们看看怎么日常开发中怎么运用单例模式。

  • 第一种写法:

    + (id)sharedInstance {
        static testClass *sharedInstance = nil;
        
        // 为了在多线程中也保证只产生一个实例,加上线程同步锁
        @synchronized(self) {
            if (!sharedInstance) {
                sharedInstance = [[self alloc] init];
            }
        }
        
        return sharedInstance;
    }
    
  • 第二种写法:

    + (instancetype)shareTools
    {
        return [[self alloc] init];
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone
    {
        // 注意: 单例是不可以继承的, 如果继承引发问题
        // 如果先创建父类, 那么永远都是父类
        // 如果先创建子类, 那么永远都是子类
        static id instance = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [super allocWithZone:zone];
        });
        return instance;
    }
    

注意:同时,我们还要重写copyWithZone,mutableCopyWithZone和非ARC的方法release,retain,retainCount。

抽取单例宏

由于单例模式在我们的日常开发中使用频率非常高,为了提高代码的复用性,我们可以将单例模式的代码抽取成一个宏,这样以后使用的时候可以用宏快速实现单例模式。

将下面的代码写到Singleton.h文件中,以后用到单例的时候,直接将Singleton.h添加到项目中即可快速实现单例。

使用方法:在要实现单例类的.h文件声明中写下SingleInterface(*name*),.m文件中SingleImplementation(*name*),就实现了单例模式。

#define SingleInterface(name) +(instancetype)share##name

#if __has_feature(objc_arc)
// ARC
#define SingleImplementation(name)  +(instancetype)share##name \
{                                                               \
    return [[self alloc] init];                                 \
}                                                               \
static id _instance;                                            \
+ (instancetype)allocWithZone:(struct _NSZone *)zone            \
{                                                               \
    static dispatch_once_t onceToken;                           \
    dispatch_once(&onceToken, ^{                                \
        _instance = [super allocWithZone:zone];                 \
    });                                                         \
    return _instance;                                           \
}                                                               \
- (id)copyWithZone:(NSZone *)zone                               \
{                                                               \
    return self;                                                \
}                                                               \
- (id)mutableCopyWithZone:(NSZone *)zone                        \
{                                                               \
    return self;                                                \
}

#else

// MRC
#define SingleImplementation(name)  +(instancetype)share##name  \
{                                                               \
    return [[self alloc] init];                                 \
}                                                               \
static id _instance;                                            \
+ (instancetype)allocWithZone:(struct _NSZone *)zone            \
{                                                               \
    static dispatch_once_t onceToken;                           \
    dispatch_once(&onceToken, ^{                                \
        _instance = [super allocWithZone:zone];                 \
    });                                                         \
    return _instance;                                           \
}                                                               \
- (id)copyWithZone:(NSZone *)zone                               \
{                                                               \
    return self;                                                \
}                                                               \
- (id)mutableCopyWithZone:(NSZone *)zone                        \
{                                                               \
    return self;                                                \
}                                                               \
- (oneway void)release                                          \
{}                                                              \
- (instancetype)retain                                          \
{                                                               \
    return self;                                                \
}                                                               \
- (NSUInteger)retainCount                                       \
{                                                               \
    return MAXFLOAT;                                            \
}
#endif```

相关文章

  • 单例模式 Singleton Pattern

    单例模式-菜鸟教程 iOS中的设计模式——单例(Singleton) iOS-单例模式写一次就够了 如何正确地写出...

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • iOS模式设计之--创建型:1、单例模式

    iOS模式设计之--1、单例模式

  • 谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以...

  • iOS单例设计模式

    在iOS的开发中,单例设计模式(Singleton Pattern)是用得最多的设计模式之一了。在iOS的SDK中...

  • 【设计模式】单例模式

    学习文章 iOS设计模式 - 单例 SwiftSingleton 原理图 说明 单例模式人人用过,严格的单例模式很...

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • IOS 设计模式

    IOS开发中几种设计模式:单例模式、观察者模式、MVC模式、代理模式 一、单例模式 场景:确保程序运行期某个类,只...

  • iOS中的单例模式

    iOS开发中常用到2中设计模式,分别是代理模式和单例模式,本文主要介绍下单例模式 单例模式的作用 可以保证在程序运...

网友评论

      本文标题:iOS中的单例设计模式

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