转自:点击打开链接

From my short experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety.

These approaches might change or become redundant as the language matures.

Global constant

let _SingletonSharedInstance = Singleton()

class Singleton  {

class var sharedInstance : Singleton {

return _SingletonSharedInstance

}

}

We use a global constant because class constants are not yet supported.

This approach supports lazy initialization because Swift lazily initializes global constants (and variables), and is thread safe by virtue of let.

Nested struct

class Singleton {

class var sharedInstance : Singleton {

struct Static {

static let instance : Singleton = Singleton()

}

return Static.instance

}

}

Unlike classes, structs do support static constants. By using a nested struct we can leverage its static constant as a class constant.

dispatch_once

The traditional Objective-C approach ported to Swift. I'd say it's no longer necessary to use this approach but I'm putting it here anyway as I find the differences in syntax interesting.

class Singleton {

class var sharedInstance : Singleton {

struct Static {

static var onceToken : dispatch_once_t = 0

static var instance : Singleton? = nil

}

dispatch_once(&Static.onceToken) {

Static.instance = Singleton()

}

return Static.instance!

}

}

Swift 实现单例模式Singleton pattern的三种方法的更多相关文章

  1. Viewing the interface of your Swift code,查看Swift代码的头文件的三种方法

      Technical Q&A QA1914 Viewing the interface of your Swift code Q:  How do I view the interface ...

  2. Swift 学习笔记 (解决Swift闭包中循环引用的三种方法)

    话不多说 直接上代码 class SmartAirConditioner { var temperature:Int = //类引用了函数 var temperatureChange:((Int)-& ...

  3. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  4. 浅谈设计模式--单例模式(Singleton Pattern)

    题外话:好久没写blog,做知识归纳整理了.本来设计模式就是个坑,各种文章也写烂了.不过,不是自己写的东西,缺少点知识的存在感.目前还没做到光看即能记住,得写.所以准备跳入设计模式这个大坑. 开篇先贡 ...

  5. 【设计模式】单例模式 Singleton Pattern

    通常我们在写程序的时候会碰到一个类只允许在整个系统中只存在一个实例(Instance)  的情况, 比如说我们想做一计数器,统计某些接口调用的次数,通常我们的数据库连接也是只期望有一个实例.Windo ...

  6. 设计模式系列之单例模式(Singleton Pattern)——确保对象的唯一性

    模式概述 模式定义 模式结构图 饿汉式单例与懒汉式单例 饿汉式单例 懒汉式单例 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 适用场景 说明:设计模式系列文章是读刘伟所著 ...

  7. 设计模式之单例模式(Singleton Pattern)

    单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...

  8. 抽象工厂(Abstract Factory),工厂方法(Factory Method),单例模式(Singleton Pattern)

    在谈工厂之前,先阐述一个观点:那就是在实际程序设计中,为了设计灵活的多态代码,代码中尽量不使用new去实例化一个对象,那么不使用new去实例化对象,剩下可用的方法就可以选择使用工厂方法,原型复制等去实 ...

  9. 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

    原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 单例模式(Singleton Pattern) 作者:weba ...

随机推荐

  1. python 日志的配置,python对日志封装成类,日志的调用

    # python 日志的配置,python对日志封装成类,日志的调用 import logging # 使用logging模块: class CLog: # --------------------- ...

  2. unity, 如果碰撞使用2d物理,为防止颤动,需将更新position的代码写在FixedUpdate里

    例如我为主角添加了一个circle collider 2d,和一个rigidbody 2d,为障碍物添加了一个circle collider 2d. 然后我在主角的Update函数里更新位置让主角由A ...

  3. Mybatis(六):spring与mybatis三种整合方法

    1.采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean. spring-mybatis.xml: <?xml ve ...

  4. 微信client内部推荐项目总结

    如今实习的公司在面向企业提供招聘服务领域数一数二,而下半年的产品重点就在于移动端微信招聘项目.而这次内推项目开发属于微信招聘一个分支.     一.内推综述     乐帝之前读<招聘与录用> ...

  5. JCO 自定义DestinationDataProvider

    要让JAVA程序能访问SAP系统,一般通过SAP JCO接口进行通讯,在获取到SAP的连接时需求提供一些连接参数,这些参数在最新的 JCO 3.0 中需要被保存到一个带有扩展名.jcoDestinat ...

  6. zookeeper单节点windows下安装

    由于需要在windows下面安装zookeeper,故做个整理 1.下载zookeeper http://mirrors.hust.edu.cn/apache/zookeeper/ 2.解压 3.修改 ...

  7. 总结iOS9中的新的方法

    iOS平台在快速的发展,各种接口正在不断的更新.随着iOS9的发布,又有一批老方法不推荐使用了,你若调用这些方法,运行的结果是没有问题的,但是会出现警告“***is deprecated :first ...

  8. PHP flock() 函数 php中的文件锁定机制

    举一个例子: 假设一个文件读取的过程,有数万人在同时操作,那么极可能a用户刚刚写入,b用户也写入了,两者以至于混乱,或者在读取的时候,读取到别人写的数据.就好比上公共厕所,去厕所的时候要把门给打开上, ...

  9. HttpURLConnection的使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; ...

  10. sama5d3 环境检测 gpio--yk测试

    说明: gpio的MAP关系 yk0--pioA7  yk1--pioA5   yk2--pioA9   yk3--pioA3   yk4--pioA1  yk5--pioA8    (端子从左--& ...