Kamaropoulos / COVID19Py

A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data.
https://pypi.org/project/COVID19Py/
GNU General Public License v3.0
84 stars 57 forks source link

Singleton Creation Design Pattern Applied #118

Closed Shaqatk closed 2 years ago

Shaqatk commented 3 years ago

Singleton Pattern Applied

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.