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!

}

}

时间: 2024-08-29 00:42:48

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 of my Swift code in Xcode? 问: 怎么在Xcode中查看swift文件的头文件? A:?Xcode generates an interface file that includes all your source code's internal and public declar

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

单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using System.Collections.Generic; using System.Text; namespace Pattern.Singleton { /// <summary> /// 泛型实现单例模式 /// </summary> /// <typeparam name=&q

设计模式之单例模式Singleton pattern

单例模式Singleton pattern 一种软件设计模式.在核心结构中只包含一个被称为单例的特殊类. 一个类只有一个对象实例,并且自行实例化向整个系统提供. 动机 一个系统中可以存在多个打印任务,但是只能有一个正在工作的任务:一个系统只能有一个窗口管理器或文件系统:一个系统只能有一个计时工具或ID(序号)生成器.如在Windows中就只能打开一个任务管理器.如果不使用机制对窗口对象进行唯一化,将弹出多个窗口,如果这些窗口显示的内容完全一致,则是重复对象,浪费内存资源:如果这些窗口显示的内容不

ninject学习笔记二:单例模式singleton pattern

今天准备学习singleton pattern,顾单词思含义,就是一个实例的意思.单例的实现思路是:私有化构造函数,提供公有方法获取类的实例.下面定义一个音乐播放器类MusicPlayer,观察单例的实现方法 class MusicPlayer { private static readonly MusicPlayer player = new MusicPlayer(); private MusicPlayer() { } public static MusicPlayer GetInstan

Swift中,把NSData转换为Byte数组的三种方法

最近做IOS开发需要从一个NSData中读出数据.但是找不到相关文档.网上搜索也有人问,我看到有Objective-C的答案,但基本上没有Swift的方案. NSData的bytes是UnsafePointer<Void>类型,我不知道怎样直接获取到他的值(如果你知道,请一定要告诉我). 所以我自己摸索了一下把NSData转换为[Byte]的方法,希望起到抛砖引玉的作用. 下面直接在PlayGround中演示: 最后我采用的当然是第三种方法: import Foundation var b:[

Spring框架笔记(十一)——IOC容器创建bean实例的第三种方法——FactoryBean

IOC容器创建bean实例有3类方法: 1 通过bean对应实例的全类名 2 通过工厂方法,包括静态工厂和实例工厂 3 实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean 今天我们介绍这第三种方法--FactoryBean Spring 中有两种类型的 Bean, 一种是普通Bean, 另一种是工厂Bean, 即FactoryBean. 工厂 Bean 跟普通Bean不同, 其返回的对象不是指定类的一个实例, 其返回的是该工厂 Bean 的 getObject 方法

防止sql注入的三种方法

常用的避免SQL注入的三种方法 一,存储过程 在学习数据库视频的时候接触过,它是存储在数据库中的一些事先编译好的指令.在用的时候不用重新编写,直接调用就好了.所以,使用它可以大大提高程序的执行效率. 那么,如果创建一个存储程序并使用它呢?这是我们今天要解决的问题 1.创建过程 可编程性--下拉菜单--存储过程--右键--查询菜单--指定模板参数的值--新建查询--输入语句--查询菜单中的分析检查语法是否正确--执行 2.具体创建语法 在创建存储程序时,为了应对各种变换的数据,通常会涉及到带参数的

Java实现ping功能的三种方法

Java实现ping功能的三种方法 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要使用java来实现ping功能. 为了使用java来实现ping的功能,有人推荐使用java的 Runtime.exec()方法来直接调用系统的Ping命令,也有人完成了纯Java实现Ping的程序,使用的是Java的NIO包(native io, 高效IO包).但是设备检测只是想测试一个远程主机是否可用.所以,可以使用以下三种方式来实现: 1.Jdk1.5的InetAddresss方式 自从Jav

Java实现ping功能的三种方法及Linux的区分

前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要使用java来实现ping功能. 为了使用java来实现ping的功能,有人推荐使用java的 Runtime.exec()方法来直接调用系统的Ping命令,也有人完成了纯Java实现Ping的程序,使用的是Java的NIO包(native io, 高效IO包).但是设备检测只是想测试一个远程主机是