//XCode Playground Friendly
/**
Singleton Pattern - Ensures that only one instance of a object exists in the application context
usefull for things that will have a global context across the app.
Ex: An internet connection, Credentials, etc.
* Can only be used for reference types.
*/
/*
Implementation using private initializer - This is threadsafe and the simplest implementation
*/
class Singleton {
static let sharedInstance = Singleton() //<- Singleton Instance
var instanceNum: Int = 0
private init() { /* Additional instances cannot be created */ }
}
/* Test Code */
let instance1 = Singleton.sharedInstance
let instance2 = Singleton.sharedInstance
instance1.instanceNum = 1
instance2.instanceNum = 2
assert(instance1.instanceNum == 2)