Skip to content

Commit 96e5c9f

Browse files
committed
Apply singleton pattern to data source
1 parent 8426e97 commit 96e5c9f

File tree

1 file changed

+88
-17
lines changed

1 file changed

+88
-17
lines changed

app/data/__init__.py

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,102 @@
11
"""app.data"""
2-
from ..services.location import LocationService
2+
from abc import ABC, abstractmethod
33
from ..services.location.csbs import CSBSLocationService
44
from ..services.location.jhu import JhuLocationService
55
from ..services.location.nyt import NYTLocationService
66

7-
class dataSourceFactory():
8-
def getDataSource():
9-
return LocationService()
7+
class DataSourceFactory(ABC):
8+
def __init__(self):
9+
pass
10+
11+
@abstractmethod
12+
def getInstance():
13+
pass
1014

11-
class JhuFactory(dataSourceFactory):
12-
def getDataSource():
13-
return JhuLocationService()
15+
@abstractmethod
16+
def getService():
17+
pass
1418

15-
class CSBSFactory(dataSourceFactory):
16-
def getDataSource():
17-
return CSBSLocationService()
19+
class JhuFactory(DataSourceFactory):
20+
__instance = None
21+
__service = None
1822

19-
class NYTFactory(dataSourceFactory):
20-
def getDataSource():
21-
return NYTLocationService()
23+
def __init__(self):
24+
if JhuFactory.__instance != None:
25+
raise Exception("Factory is singleton!")
26+
elif JhuFactory.__service != None:
27+
raise Exception("Service is singleton!")
28+
else:
29+
JhuFactory.__instance = self
30+
JhuFactory.__service = JhuLocationService()
31+
32+
@staticmethod
33+
def getInstance():
34+
if JhuFactory.__instance == None:
35+
JhuFactory()
36+
return JhuFactory.__instance
37+
38+
@staticmethod
39+
def getService():
40+
if JhuFactory.__service == None:
41+
JhuFactory()
42+
return JhuFactory.__service
43+
44+
class CSBSFactory(DataSourceFactory):
45+
__instance = None
46+
__service = None
47+
48+
def __init__(self):
49+
if CSBSFactory.__instance != None:
50+
raise Exception("Factory is singleton!")
51+
elif CSBSFactory.__service != None:
52+
raise Exception("Service is singleton!")
53+
else:
54+
CSBSFactory.__instance = self
55+
CSBSFactory.__service = CSBSLocationService()
56+
57+
@staticmethod
58+
def getInstance():
59+
if CSBSFactory.__instance == None:
60+
CSBSFactory()
61+
return CSBSFactory.__instance
62+
63+
@staticmethod
64+
def getService():
65+
if CSBSFactory.__service == None:
66+
CSBSFactory()
67+
return CSBSFactory.__service
68+
69+
class NYTFactory(DataSourceFactory):
70+
__instance = None
71+
__service = None
72+
73+
def __init__(self):
74+
if NYTFactory.__instance != None:
75+
raise Exception("Factory is singleton!")
76+
elif NYTFactory.__service != None:
77+
raise Exception("Service is singleton!")
78+
else:
79+
NYTFactory.__instance = self
80+
NYTFactory.__service = NYTLocationService()
81+
82+
@staticmethod
83+
def getInstance():
84+
if NYTFactory.__instance == None:
85+
NYTFactory()
86+
return NYTFactory.__instance
87+
88+
@staticmethod
89+
def getService():
90+
if NYTFactory.__service == None:
91+
NYTFactory()
92+
return NYTFactory.__service
2293

2394

2495
# Mapping of factories to data-sources.
2596
DATA_SOURCES = {
26-
"jhu": JhuFactory(),
27-
"csbs": CSBSFactory(),
28-
"nyt": NYTFactory(),
97+
"jhu": JhuFactory.getInstance(),
98+
"csbs": CSBSFactory.getInstance(),
99+
"nyt": NYTFactory.getInstance(),
29100
}
30101

31102
def data_source(source):
@@ -35,4 +106,4 @@ def data_source(source):
35106
:returns: The service.
36107
:rtype: LocationService
37108
"""
38-
return DATA_SOURCES.get(source.lower()).getDataSource()
109+
return DATA_SOURCES.get(source.lower()).getService()

0 commit comments

Comments
 (0)