iOS&设计模式 - 单例模式(Singleton)

单例(singleton)

  • 意图

    • 保证一个类仅有一个实例,并提供一个访问它的全局访问点。
  • 适用性

    • 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它
    • 当这个唯一的实例应该是通过子类化可以扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时
  • 结构

    896CA6D0-7B09-435C-991A-6A7870746B5F.png
  • 优缺点

    • 对唯一实例的受控访问
    • 缩小命名空间
    • 允许对操作和表示的精化
    • 允许可变数目的实例
    • 比类操作更加灵活
  • 实现

    • OC版本
    // 非线程安全
    @interface Singleton: NSObject
    +(instancetype)sharedInstance;
    @end
    
    @implementation Singleton
    static Singleton* instance = nil;
    
    // 程序运行时就初始化该单例
    //+(void)load {
    //    [Singleton sharedInstance];
    //}
    
    +(instancetype)sharedInstance {
        if(instance == nil){
            instance = [[Singleton alloc] init];
        }
        return instance;
    }
    
    //当我们调用alloc时候回调改方法(保证唯一性),oc不允许构造方法私有化
    +(id)allocWithZone:(struct _NSZone *)zone {
        if(instance == nil){
            instance = [super allocWithZone:zone];
        }
        return instance;
    }
    @end
    
    // 线程安全
    @implementation Singleton
    static Singleton* instance = nil;
    +(instancetype)sharedInstance {
        if(instance == nil) { // 加锁解锁是需要消耗性能的
            @synchronized (self) {
                if(instance == nil) {
                    instance = [[Singleton alloc] init];
                }
            }
        }
        return instance;
    }
    
    +(id)allocWithZone:(struct _NSZone *)zone {
        @synchronized (self) {
            if(instance == nil) {
                instance = [super allocWithZone:zone];
            }
        }
        return instance;
    }
    @end
    
    // 线程安全-GCD
    @implementation Singleton
    static Singleton* instance = nil;
    
    +(instancetype)sharedInstance {
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            instance = [[Singleton03 alloc] init];
        });
        return instance;
    }
    
    // 2017.8.10 更新,这里的做法是不对的,原因请看下文
    //当我们调用alloc时候回调改方法(保证唯一性),oc不允许构造方法私有化
    +(id)allocWithZone:(struct _NSZone *)zone {
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            instance = [super allocWithZone:zone];
        });
        return instance;
    }
    @end
    
    

    华丽的分割线,感觉慧慧大神指出 OC 版单例的一处错误,上面所以说的用 allocWithZone 来保持唯一性的做法不是太合理,应该直接禁用 alloc 和 init 方法:

    + (id)alloc NS_UNAVAILABLE;
    - (instancetype) init NS_UNAVAILABLE;
    

    • Swift版本
    // 非线程安全
    final class Singleton: NSObject {
        private static var instance: Singleton? = nil
        class func sharedInstance() -> Singleton {
            if instance == nil {
                instance = Singleton()
            }
            return instance!
        }
        // 将构造方法私有化
        private override init() {  
        }
    }
            
    // 线程安全
    final class Singleton: NSObject {
        static var sharedInstance = Singleton()
        private override init() {   
        }
    }
    
    

对于如何选择线程安全或者非线程安全的单例呢,如果程序不涉及多线程时,就优先选择非线程安全吧,毕竟加锁解锁还是需要消耗资源的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是单例模式? 单例模式是一种常用的软件设计模式。可以保证通过单例模式可以保证系统中一个类只有一个实例而且该实例...
    WakeMeUP1阅读 997评论 0 2
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    msn北极光阅读 222评论 0 0
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    不要重名就好阅读 556评论 0 0
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    VincentHK阅读 668评论 0 3
  • 单例介绍 单例模式: 一个类只有一个实例对象。 使用场合:在整个应用程序共享一份资源(这份资源只需要创建初始化一次...
    Mr在水一方阅读 326评论 0 0