Skip to content

Latest commit

 

History

History
45 lines (30 loc) · 711 Bytes

File metadata and controls

45 lines (30 loc) · 711 Bytes

Singleton

Ensure a class has only one instance, and provide a global point of access to it.

Source: wikipedia.org

Swift Example

final class Singleton {
    static let sharedInstance = Singleton()

    private init() {
        // Private initialization to ensure just one instance is created.
    }
}

Swift Usage

let singleton = Singleton.sharedInstance

Kotlin Example

object Singleton {
    init {
        println("Initializing with object: $this")
    }

    fun doSomething() = println("Do something with object: $this")
}

Kotlin Usage

Singleton.doSomething()