Skip to content

Commit 04b96b3

Browse files
committed
add Proxy pattern
1 parent 8f6ca3e commit 04b96b3

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

proxy/Proxy.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Python Design Patterns: Proxy
3+
# Author: Jakub Vojvoda [github.com/JakubVojvoda]
4+
# 2016
5+
#
6+
# Source code is licensed under MIT License
7+
# (for more details see LICENSE)
8+
#
9+
10+
import sys
11+
12+
#
13+
# Subject
14+
# defines the common interface for RealSubject and Proxy
15+
# so that a Proxy can be used anywhere a RealSubject is expected
16+
#
17+
class Subject:
18+
def request(self):
19+
pass
20+
21+
#
22+
# Real Subject
23+
# defines the real object that the proxy represents
24+
#
25+
class RealSubject(Subject):
26+
def __init__(self):
27+
Subject.__init__(self)
28+
29+
def request(self):
30+
print("Real Subject request.")
31+
32+
#
33+
# Proxy
34+
# maintains a reference that lets the proxy access the real subject
35+
#
36+
class Proxy(Subject):
37+
def __init__(self):
38+
Subject.__init__(self)
39+
self._subject = RealSubject()
40+
41+
def request(self):
42+
self._subject.request()
43+
44+
45+
if __name__ == "__main__":
46+
proxy = Proxy()
47+
proxy.request()
48+

0 commit comments

Comments
 (0)