2

Can anyone please explain what is the reason to prevent an instance in singleton.

I saw that some says to do like below

private override init (){

}

but it works fine if we do this or not.

Jobs
  • 269
  • 2
  • 6
  • 21
  • 3
    There's explanations in my answer and its comments here: http://stackoverflow.com/q/36011904/2227743 Basically you need a private init because otherwise anyone would be able to create a new instance, and if doing so then the object wouldn't be a singleton anymore (singleton, in this interpretation of the term, is a *unique* shared instance). – Eric Aya Apr 07 '17 at 10:21

1 Answers1

0

I'm not sure what do you mean by this question, if you're asking why is the initialiser private then it comes from definition of Singleton design pattern. Initialiser needs to be private to make sure nobody can crate another instance of your singleton.

beretis
  • 899
  • 1
  • 9
  • 24