Swift 实现单例模式Singleton pattern的三种方法
转自:点击打开链接
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的三种方法的更多相关文章
- 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 ...
- Swift 学习笔记 (解决Swift闭包中循环引用的三种方法)
话不多说 直接上代码 class SmartAirConditioner { var temperature:Int = //类引用了函数 var temperatureChange:((Int)-& ...
- 二十四种设计模式:单例模式(Singleton Pattern)
单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...
- 浅谈设计模式--单例模式(Singleton Pattern)
题外话:好久没写blog,做知识归纳整理了.本来设计模式就是个坑,各种文章也写烂了.不过,不是自己写的东西,缺少点知识的存在感.目前还没做到光看即能记住,得写.所以准备跳入设计模式这个大坑. 开篇先贡 ...
- 【设计模式】单例模式 Singleton Pattern
通常我们在写程序的时候会碰到一个类只允许在整个系统中只存在一个实例(Instance) 的情况, 比如说我们想做一计数器,统计某些接口调用的次数,通常我们的数据库连接也是只期望有一个实例.Windo ...
- 设计模式系列之单例模式(Singleton Pattern)——确保对象的唯一性
模式概述 模式定义 模式结构图 饿汉式单例与懒汉式单例 饿汉式单例 懒汉式单例 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 适用场景 说明:设计模式系列文章是读刘伟所著 ...
- 设计模式之单例模式(Singleton Pattern)
单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...
- 抽象工厂(Abstract Factory),工厂方法(Factory Method),单例模式(Singleton Pattern)
在谈工厂之前,先阐述一个观点:那就是在实际程序设计中,为了设计灵活的多态代码,代码中尽量不使用new去实例化一个对象,那么不使用new去实例化对象,剩下可用的方法就可以选择使用工厂方法,原型复制等去实 ...
- 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)
原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 单例模式(Singleton Pattern) 作者:weba ...
随机推荐
- Linux关机和重启命令总结
一.shutdown 命令 作用:关闭或重启系统 使用权限:超级管理员使用 常用选项 1. -r 关机后立即重启 2. -h关机后不重启 3. -f快速关机,重启时跳过fsck(file system ...
- 编译安装PHP7并安装Redis扩展Swoole扩展(未实验)
用PECL自动安装Redis扩展.Swoole扩展 pecl install redis pecl install swool 编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机 ...
- unity, 设置帧率上限
用unity做了个demo,把所有开销大的特效都去了,在真机上运行仍然卡.显示帧率来看,最高到30.原来unity在ios设备上帧率默认限制为不超过30. 可以通过Application.target ...
- spring容器ApplicationContext初始化(spring应用上下文初始化)
可以通过以下三种方式加载spring容器,实现bean的扫描与管理: 1. ClassPathXmlApplicationContext:从类路径中加载 ClassPathXmlApplication ...
- vue 的调试工具
转载自:http://www.cnblogs.com/lolDragon/p/6268345.html 安装 1.github下载地址:https://github.com/vuejs/vue-dev ...
- C++之string的底层真的是用char数组来实现的么?
一.引言 遇到一个问题:使用加密库对数据进行加密,得到密文,使用string进行保存并传输,然后可以正确解密出来,但是使用string.c_str()进行参数传递则无法正确解密出明文. 原因是:密文中 ...
- mysql 从一个表中查询插入另一个表
insert into dnt_userfields (uid,realname ) select uid,nickname from discuz.dnt_users where uid>72 ...
- 高效学习Oracle的方法论
Oracle的很多经验并不是来自工业环境.很多问题和经验都可以从自己的测试里积累 实验要做,但也要想! 那思维的起点是什么? 以下是我的看法.或者有些不合理: ...
- jQuery插件开发全解析<转>
jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...
- PHP——动态随机数
取1-13随机数 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...