- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
Latest commit
48 lines (37 loc) · 1.13 KB
/
Copy pathproxy.py
File metadata and controls
48 lines (37 loc) · 1.13 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
fromabcimportABC, abstractmethod
# Service interface
classSubject(ABC):
@abstractmethod
defrequest(self):
pass
# ConcreteService
classRealSubject(Subject):
defrequest(self):
print("RealSubject: Handling request.")
# ProxyService
classProxy(Subject):
def__init__(self):
self._real_subject=RealSubject() # ConcreteService
defrequest(self):
# Access control or additional logic can be added here
print("Proxy: Checking access before handling request.")
self._real_subject.request()
# Client code
defclient_code(subject: Subject):
subject.request()
# Usage
defmain():
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.