-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathch_11_virtual_proxy.py
50 lines (37 loc) · 1.17 KB
/
ch_11_virtual_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Icon:
@property
def width(self) -> int:
raise NotImplementedError
@property
def height(self) -> int:
raise NotImplementedError
def paint_icon(self) -> None:
raise NotImplementedError
class ImageIcon(Icon):
@property
def width(self) -> int:
return 1280
@property
def height(self) -> int:
return 720
def paint_icon(self) -> None:
print(":)")
class ImageProxy(Icon):
def __init__(self, url: str):
self._image_icon = None
self._url = url
# Following 'if' statements can be reworked to use The State Pattern: ImageNotLoaded and ImageLoaded
@property
def width(self) -> int:
return self._image_icon.width if self._image_icon else 600
@property
def height(self) -> int:
return self._image_icon.height if self._image_icon else 800
def paint_icon(self) -> None:
if not self._image_icon:
# Download image from the internet
print(f"Downloading the image from '{self._url}'")
self._image_icon = ImageIcon()
self._image_icon.paint_icon()
image = ImageProxy("whatever://image")
image.paint_icon()