As we discussed in the class the right thing to
do here was to use the factory pattern. So here is
a possible code to solve the problem. Of course that there
are different possible solutions. So what we did for this code
was to first create a private, static, Singleton object called
instance, which is the one that will keep track of the
only instance that can be created on the class. Then we
define the default constructor, the constructor that doesn't take any parameter
as private. In this way other classes cannot create instances
of Singleton without calling our factory method, and finally we
create the factory method. And the factory method is very
simple. The method will first check whether an instance of
the class was already created. If it was created, it
would just return that instance. Otherwise, it will create a
new instance and assign it to that instance member variable
and then return the newly created instance. So with this code
you're guaranteed that other classes cannot bypass the factory
method, because the default constructor is private. And the that
the factory method will create one and only one instance
of the class, which is exactly what our requirements were.