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

Creational Pattern #128

Closed mattbocc closed 3 years ago

mattbocc commented 3 years ago

I implemented a singleton creational pattern in the COVID19 class in order to make sure there's only one instance of the class made. Making multiple instances of this class is redundant as you can find all the information you need from within the first instance you create. All the information methods can be accessed from a single instance of the class.

I applied the singleton creational pattern by creating a static method called getAccessToGlobal() which will act as the access point to the single instance of COVID19 as well as a means to instantiate it (if it hasn't been instantiated already). Static methods make this possible by having a way to access getAccessToGlobal() without having to create an instance of COVID19. Therefore letting us stick to the rules of the singleton creational pattern. When called upon, getAccessToGlobal() will check if COVID19._onlyInstance exists (which is the only instance to be instantiated). If it does exist it will return the instance, otherwise it'll create an instance of COVID19, then return it.

You can check my test file for reference to how this would all work from the users end.