This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
proxy.py
63 lines (45 loc) · 1.73 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Proxy pattern
Proxy is a structural design pattern. A proxy is a surrogate object which can
communicate with the real object (aka implementation). Whenever a method in the
surrogate is called, the surrogate simply calls the corresponding method in
the implementation. The real object is encapsulated in the surrogate object when
the latter is instantiated. It's NOT mandatory that the real object class and
the surrogate object class share the same common interface.
"""
from abc import ABC, abstractmethod
class CommonInterface(ABC):
"""Common interface for Implementation (real obj) and Proxy (surrogate)."""
@abstractmethod
def load(self):
pass
@abstractmethod
def do_stuff(self):
pass
class Implementation(CommonInterface):
def __init__(self, filename):
self.filename = filename
def load(self):
print("load {}".format(self.filename))
def do_stuff(self):
print("do stuff on {}".format(self.filename))
class Proxy(CommonInterface):
def __init__(self, implementation):
self.__implementation = implementation # the real object
self.__cached = False
def load(self):
self.__implementation.load()
self.__cached = True
def do_stuff(self):
if not self.__cached:
self.load()
self.__implementation.do_stuff()
def main():
p1 = Proxy(Implementation("RealObject1"))
p2 = Proxy(Implementation("RealObject2"))
p1.do_stuff() # loading necessary
p1.do_stuff() # loading unnecessary (use cached object)
p2.do_stuff() # loading necessary
p2.do_stuff() # loading unnecessary (use cached object)
p1.do_stuff() # loading unnecessary (use cached object)
if __name__ == "__main__":
main()