From the course: Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

How the pattern works

How the pattern works

From the course: Design Patterns: Creational

Start my 1-month free trial

How the pattern works

- [Instructor] The class diagram for Singleton is deceptively simple. After all, it has just one class. The trick for any Singleton is to make sure that only one instance is created. In the class diagram, you can see that the idea is to have a static unique instance that will hold the one and only instance of the Singleton class. In a language like Java, this is what's called a class variable. The variable exists only in the class, not in instances of the class. So given that there is only ever one Singleton class in a package, this means there's only one unique instance variable for Singleton. Notice too, that we have a private constructor for Singleton. That means that the only class that can call the private constructor to make an instance of this class is the Singleton itself. We can initialize the unique instance variable that's going to hold the one and only instance of Singleton by assigning it a new…

Contents