Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding singleton pattern on services
  • Loading branch information
milandrapic committed Aug 16, 2021
commit a4f821ab2d3ddc6ea1c14e3816dc62da9247a878
6 changes: 3 additions & 3 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

# Mapping of services to data-sources.
DATA_SOURCES = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
"jhu": JhuLocationService.getInstance(),
"csbs": CSBSLocationService.getInstance(),
"nyt": NYTLocationService.getInstance(),
}


Expand Down
15 changes: 15 additions & 0 deletions app/services/location/csbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ class CSBSLocationService(LocationService):
Service for retrieving locations from csbs
"""

__instance = None

@staticmethod
def getInstance():
if CSBSLocationService.__instance == None:
CSBSLocationService()
return CSBSLocationService.__instance

def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self

async def get_all(self):
# Get the locations.
locations = await get_locations()
Expand Down
15 changes: 15 additions & 0 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ class JhuLocationService(LocationService):
Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).
"""

__instance = None

@staticmethod
def getInstance():
if CSBSLocationService.__instance == None:
CSBSLocationService()
return CSBSLocationService.__instance

def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self

async def get_all(self):
# Get the locations.
locations = await get_locations()
Expand Down
16 changes: 16 additions & 0 deletions app/services/location/nyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ class NYTLocationService(LocationService):
Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data).
"""


__instance = None

@staticmethod
def getInstance():
if CSBSLocationService.__instance == None:
CSBSLocationService()
return CSBSLocationService.__instance

def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self

async def get_all(self):
# Get the locations.
locations = await get_locations()
Expand Down