ExpDev07 / coronavirus-tracker-api

🦠 A simple and fast (< 200ms) API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak. It's written in python using the 🔥 FastAPI framework. Supports multiple sources!
https://coronavirus-tracker-api.herokuapp.com
GNU General Public License v3.0
1.59k stars 320 forks source link

Creational pattern on data source #477

Closed antheia-z closed 3 years ago

antheia-z commented 3 years ago

The factory and singleton patterns were introduced to the data source to make it easier to modify the code when changes need to be made to the data source and to reduce memory usage.

To do this, I first created a factory called DataSourceFactory, including getInstance and getService methods. After that, I created subclasses of the factory for each of the three data sources. Then, I make the factory a singleton by using the getInstance method, which creates a new object the first time it is called and stores it in a static variable. The method returns the instance each time it is called after that. In addition, I made the Service a singleton by using getService, which is implemented in the same way as getInstance.