Skip to main content
added 1 more scenario
Source Link
geekay
  • 111
  • 4

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()

UPDATE Just for a variation if you need to initialize some member variables in your singleton. You can do something like :

    public static let sharedInstance: FGSingleton = {
    let objBirthDate: NSDate = NSDate()
    return FGSingleton(date: objBirthDate)
}()

required private init(data: NSDate){...}

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()

UPDATE Just for a variation if you need to initialize some member variables in your singleton. You can do something like :

    public static let sharedInstance: FGSingleton = {
    let objBirthDate: NSDate = NSDate()
    return FGSingleton(date: objBirthDate)
}()

required private init(data: NSDate){...}
fixed code snippets
Source Link
Quill
  • 12.1k
  • 5
  • 41
  • 94

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstanceprivate sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()
Source Link
geekay
  • 111
  • 4

Most of the other answers cover everything in a very detailed and accurate manner but I want to highlight some of the practices that I liked to preach when it comes to singleton classes.

I used to create a singleton and then all the public methods in the class were static. These static methods called the then call the replica instance methods using the private sharedInstance

As its a singleton all my method calls are going to be using shared instance ONLY so why specify it again and again.

class FGSingleton {
private static let sharedInstance = FGSingleton()
private var gameScore: Int = 0

// METHODS
private init() {
    println(__FUNCTION__)
}
class func displayGameScore() {
    println("\(__FUNCTION__) \(self.sharedInstance.gameScore)")
}
class func incrementGameScore(scoreInc: Int) {
    self.sharedInstance.gameScore += scoreInc
}
}

and call will be simply

  FGSingleton.incrementGameScore(5)
  FGSingleton.displayGameScore()