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 323 forks source link

Add a creational pattern #451

Closed FrankX-xzh closed 3 years ago

FrankX-xzh commented 3 years ago

Why I made the specific design decision: I apply a singleton pattern in the SUD to ensure the LocationService of each data-source has at most one instance and provide a global access point to that instance to avoid bugs arising from multiple instances. And the singleton pattern can ensure only one instance to save memory which can improve software performance. So I chose the singleton pattern. How I applied the new design: I used the decorator to implement the singleton pattern in app\services\location_init_.py. Define a dictionary in the decorator to hold instances. Then every time create an instance, check it in the dictionary. If it has already instantiated, take the instance directly. If not, create an instance and store it in the dictionary. Then add “@singleton” to LocationService class to ensure each data-source has at most one instance.