In the process of writing the code,We will meet when the need to ensure that only one instance of the global,This time use the singleton pattern。
Single mode--this ensures that only one instance of a class,And provide a global point of access to。
What about how to implement the singleton pattern? We take a look at the classic Java code,How to implement the singleton pattern。
Our online search for "Java singleton pattern" will be able to find plenty of readily available code snippets,Here are random excerpts of a report:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.whut.singleton; public class SingletonObject { private static SingletonObject singleObject; private SingletonObject(){} public static synchronized SingletonObject getInstance() { if(singleObject==null) singleObject=new SingletonObject(); return singleObject; } } |
Then,How to use the singleton pattern in the Swift?
In accordance with the "common sense",You may be written like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import Foundation class Singleton { private init() { print("Singleton inited!") } static var singleton:Singleton? class func getSingleton()->Singleton { if let s = singleton { return s } singleton = Singleton() return singleton! } } |
In theory, "Singleton inited!" Output only once,But it really is this you?
We have a try:
1 2 3 4 5 |
import Foundation let a = Singleton.getSingleton() let b = Singleton.getSingleton() let c = Singleton.getSingleton() |
The output results are as follows:
1 2 |
Singleton inited! Program ended with exit code: 0 |
It didn't seem to matter,Then we try again:
1 2 3 4 5 6 7 8 9 |
import Foundation let background = NSOperationQueue()//获取一个后台队列 var a:Singleton? for _ in 1...1000 { background.addOperationWithBlock{ a = Singleton.getSingleton() } } |
Here we simply get the background queue,Then concurrently executed 1000 times access the instance,So in theory should also output a "Singleton inited!" The remaining 999 times,Are repeated access to the same instance,So true is it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Singleton inited! Program ended with exit code: 0 |
Obviously not-but it's not be reinitialized each time – that is to say,In the case of concurrent,This way is not safe,There is some probability that could result in duplicate initialization of,Then put into practice environment,Resulting in highly concurrent cases your data is reset、Empty。
Swift in the singleton pattern
So what exactly is the how to thread the thread safe? We change the code before:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import Foundation class Singleton { private init() { print("Singleton inited!") } static let singleton:Singleton? = Singleton() // class func getSingleton()->Singleton { // if let s = singleton { // return s // } // singleton = Singleton() // return singleton! // } } |
Here we remove the getSingleton() This Getter ,Directly in the static property to initialize its own,Here is using the lazy characteristic of Swift,It will be in time for the first class,Keep references after the。
So how should I use it? We just changed the test code:
1 2 3 4 5 6 7 8 9 |
import Foundation let background = NSOperationQueue()//获取一个后台序列 var a:Singleton? for _ in 1...1000 { background.addOperationWithBlock{ a = Singleton.singleton } } |
Where the highlighted row is modified,Same with 1000,Program execution results:
1 2 |
Singleton inited! Program ended with exit code: 0 |
Finally,Implementation of results and we expect the same。
Original article written by LogStudio:R0uter's Blog » Swift in the single mode
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1734.html