A Singleton pattern was applied to covid.py by creating a new class Singleton. This new Singleton class was the metaclass of the COVID19 class and defined how it would behave.
The Singleton class ensures that the COVID19 class has only one instance created. This is done by adding a private static field _singletonInstances that is an empty dictionary to store the instance in. Multiple instances can be created but they will always point to the same instance that was initially created.
Inside the __call__ method it creates a new object instance on the first call as there is nothing in the _singletonInstances dictionary and stores it there. After this call, it will always return the initially created instance on all the following calls.
Singleton Pattern Applied
A Singleton pattern was applied to covid.py by creating a new class
Singleton
. This newSingleton
class was the metaclass of the COVID19 class and defined how it would behave.The
Singleton
class ensures that the COVID19 class has only one instance created. This is done by adding a private static field_singletonInstances
that is an empty dictionary to store the instance in. Multiple instances can be created but they will always point to the same instance that was initially created.Inside the
__call__
method it creates a new object instance on the first call as there is nothing in the_singletonInstances
dictionary and stores it there. After this call, it will always return the initially created instance on all the following calls.