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

Applied creational pattern and Encapsulation #94

Closed pranishnepal closed 3 years ago

pranishnepal commented 3 years ago

Applied Singleton Design Pattern and Abstraction

I applied the singleton design pattern to the project. Given that the project is an API package and any request, regardless of single/multiple instances, would fetch the same data, The Singleton pattern is applicable here. This avoids concurrency issues and prevents superfluous bugs that may arise from having multiple instances. I also applied encapsulation to the project.

How?

I created a new class, SingletonPatternType and made the old COVID19 class a subclass of the new SingletonPatternType class. SingletonPatternType ensures that only one instance is ever created for all classes that are its subclasses. I added a new RequestData class that handles all the requests made in the package. Further, I added contracts and comments to functions that did not have descriptions or typehints. The tests are written using the pytest package. I have updated the requirements.txt file to install the additional packages used.

Testing

I created a test_singleton.py test file that tests if the singleton pattern was correctly implemented. To run the test, run pytest command in terminal, when inside the COVID19Py directory.

Is there any existing behaviour change due to this modification?

No, nothing changes. The code is used the same as before and behaves the same as well.