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

Implemented Singleton design pattern along with updateDataSource() and updateURL() functions #122

Closed Fentue closed 3 years ago

Fentue commented 3 years ago

Implemented Singleton design pattern for the main COVID class to ensure that there can be at most 1 instance of the covid19 class. Also added a function to update the data source and a function to update the url, so there would be no need to have more than 1 instance of the class. Before these functions were added, the only reason for there to create more than 1 instance would be to retrieve data from a different data source or url. The Singleton pattern is applied to save memory by only having 1 instance of the class and the functions are implemented to be able to update the data sources and url for that one instance, which could only be previously done by creating multiple instances. To apply the Singleton pattern, a function outside the COVID19 class was written with a dictionary being used to track whether or not an instance has already been created for the class. If an instance has been created (COVID19 being already in the dictionary), then the instance will be returned (through accessing the dictionary). If the instance has not been created (not found in the dictionary), then an instance is created, added to the dictionary, and then returned. The COVID19 class then has a decorator to the singleton function. To write the updateURL() and updateDataSource() functions, if the new url/new data source passed into the functions are the same as the current url/data source, nothing would happen aside from a prompt showing up. Otherwise, make the data source equal to the new data source (if it is valid) and make the url equal to the new url/load the mirrors if it is the default url as shown in the the init function previously.