Repository navigation
Expand file tree
/
Copy pathproxy.py
More file actions
52 lines (38 loc) · 1.47 KB
/
Copy pathproxy.py
File metadata and controls
52 lines (38 loc) · 1.47 KB
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
51
52
"""
Proxy — a stand-in that controls access to the real object
Loading an image from disk is expensive, so `ImageProxy` defers it.
It answers the same `.display()` message as `RealImage`, but only
builds the real image the first time it is actually shown — and reuses
it afterwards. The caller talks to the proxy exactly as it would to
the real thing.
Compare with the procedural Python version:
if self._real is None:
self._real = RealImage(self._filename)
return self._real.display()
POOP forbids `is None` for this. The lazy-load becomes `if_none`: the
first call finds `none` and builds the image; later calls find the
real image and return it untouched.
Smalltalk:
ImageProxy>>display
real := real ifNil: [RealImage on: filename].
^real display
"""
class RealImage:
def __init__(self, filename):
self._filename = filename
("loaded " + filename + " from disk").print()
def display(self):
return "showing " + self._filename
class ImageProxy:
def __init__(self, filename):
self._filename = filename
self._real = None
def display(self):
self._real = self._real.if_none(lambda: RealImage(self._filename))
return self._real.display()
image = ImageProxy("photo.png")
"proxy created — nothing loaded yet".print()
# First display triggers the costly load...
image.display().print()
# ...the second reuses the already-loaded image (no second "loaded" line).
image.display().print()