-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
48 lines (37 loc) · 1.13 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
from abc import ABC, abstractmethod
# Service interface
class Subject(ABC):
@abstractmethod
def request(self):
pass
# ConcreteService
class RealSubject(Subject):
def request(self):
print("RealSubject: Handling request.")
# ProxyService
class Proxy(Subject):
def __init__(self):
self._real_subject = RealSubject() # ConcreteService
def request(self):
# Access control or additional logic can be added here
print("Proxy: Checking access before handling request.")
self._real_subject.request()
# Client code
def client_code(subject: Subject):
subject.request()
# Usage
def main():
real_subject = RealSubject()
proxy = Proxy()
print("Client: Directly interacting with the RealSubject:")
client_code(real_subject)
print("\nClient: Indirectly interacting with the Proxy:")
client_code(proxy)
if __name__ == "__main__":
main()
## Output
# Client: Directly interacting with the RealSubject:
# RealSubject: Handling request.
# Client: Indirectly interacting with the Proxy:
# Proxy: Checking access before handling request.
# RealSubject: Handling request.