Skip to content

Commit 988bda1

Browse files
committed
Structural Design Pattern: Bridge Pattern
1 parent 209f36f commit 988bda1

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

app/data/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""app.data"""
2-
from ..services.location.csbs import CSBSLocationService
3-
from ..services.location.jhu import JhuLocationService
4-
from ..services.location.nyt import NYTLocationService
2+
from ..services.location.csbs import CSBSLocationServicee
3+
from ..services.location.jhu import JhuLocationServicee
4+
from ..services.location.nyt import NYTLocationServicee
5+
from app.services.location.__init__ import LocationServicer
56

67
class DataSourceSingletonMeta(type):
78
"""
8-
access point to the DataSourceSingleton
9+
access point to the DataSource
910
"""
11+
1012
_instances = {}
1113

1214
def __call__(cls, *args, **kwargs):
@@ -22,9 +24,9 @@ def __call__(cls, *args, **kwargs):
2224

2325
class DataSourceSingleton(metaclass=DataSourceSingletonMeta):
2426
DATA_SOURCES = {
25-
"jhu": JhuLocationService(),
26-
"csbs": CSBSLocationService(),
27-
"nyt": NYTLocationService(),
27+
"jhu": LocationServicer(JhuLocationServicee()),
28+
"csbs": LocationServicer(CSBSLocationServicee()),
29+
"nyt": LocationServicer(NYTLocationServicee()),
2830
}
2931
def get_data_source(self, dataSource):
3032
return self.DATA_SOURCES.get(dataSource.lower())

app/services/location/__init__.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22
from abc import ABC, abstractmethod
33

44

5-
class LocationService(ABC):
5+
class LocationServicer: # The 'Remote' of the Pattern
66
"""
7-
Service for retrieving locations.
7+
Abstraction class handling the 'Front End' portion of the project
88
"""
9-
109
@abstractmethod
11-
async def get_all(self):
12-
"""
13-
Gets and returns all of the locations.
10+
def __init__(self, implementation):
11+
self.implementation = implementation
12+
13+
@abstractmethod
14+
def all_locations(self):
15+
return self.implementation.get_all()
16+
17+
@abstractmethod
18+
def locations(self):
19+
return self.implementation.get()
20+
21+
class LocationServicee: # The 'Device' with the implmentation
22+
"""
23+
Implementation Interface
24+
"""
25+
26+
def get_all(self):
27+
# No implementation - interface method
28+
pass
29+
30+
def get(self):
31+
# No implementation - interface method
32+
pass
33+
1434

15-
:returns: The locations.
16-
:rtype: List[Location]
17-
"""
18-
raise NotImplementedError
1935

20-
@abstractmethod
21-
async def get(self, id): # pylint: disable=redefined-builtin,invalid-name
22-
"""
23-
Gets and returns location with the provided id.
24-
25-
:returns: The location.
26-
:rtype: Location
27-
"""
28-
raise NotImplementedError

app/services/location/csbs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from ...coordinates import Coordinates
1111
from ...location.csbs import CSBSLocation
1212
from ...utils import httputils
13-
from . import LocationService
13+
from . import LocationServicee
1414

1515
LOGGER = logging.getLogger("services.location.csbs")
1616

1717

18-
class CSBSLocationService(LocationService):
18+
class CSBSLocationServicee(LocationServicee):
1919
"""
2020
Service for retrieving locations from csbs
2121
"""
@@ -30,7 +30,6 @@ async def get(self, loc_id): # pylint: disable=arguments-differ
3030
locations = await self.get_all()
3131
return locations[loc_id]
3232

33-
3433
# Base URL for fetching data
3534
BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv"
3635

app/services/location/jhu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
from ...utils import countries
1616
from ...utils import date as date_util
1717
from ...utils import httputils
18-
from . import LocationService
18+
from . import LocationServicee
1919

2020
LOGGER = logging.getLogger("services.location.jhu")
2121
PID = os.getpid()
2222

2323

24-
class JhuLocationService(LocationService):
24+
class JhuLocationServicee(LocationServicee):
2525
"""
2626
Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19).
2727
"""

app/services/location/nyt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from ...location.nyt import NYTLocation
1212
from ...models import Timeline
1313
from ...utils import httputils
14-
from . import LocationService
14+
from . import LocationServicee
1515

1616
LOGGER = logging.getLogger("services.location.nyt")
1717

1818

19-
class NYTLocationService(LocationService):
19+
class NYTLocationServicee(LocationServicee):
2020
"""
2121
Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data).
2222
"""

0 commit comments

Comments
 (0)