I am confused when I read this (regarding singleton design pattern):
How do we ensure that a class has only one instance and that the instance is easily
accessible? A global variable makes an object accessible, but it doesn't keep you from
instantiating multiple objects.
So what is the use of singleton pattern if we can create multiple instances?
SOURCE:
Design Patterns – Elements Of Reusable Object Oriented Software (1995) – Gamma, Helm, Johnson, Vl
Best Answer
Without the full text this is not sure, but my (somehow educated) guess:
They only warn that a global variable is not the right way to ensure that you have a singleton. The following text should then show how to do this inside the class that should be a singleton.