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.
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 oldCOVID19
class a subclass of the newSingletonPatternType
class.SingletonPatternType
ensures that only one instance is ever created for all classes that are its subclasses. I added a newRequestData
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 thepytest
package. I have updated therequirements.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, runpytest
command in terminal, when inside theCOVID19Py
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.