MCPcopy Create free account
hub / github.com/RefactoringGuru/design-patterns-python / Proxy

Class Proxy

src/Proxy/Conceptual/main.py:53–86  ·  view source on GitHub ↗

EN: The Proxy has an interface identical to the RealSubject. RU: Интерфейс Заместителя идентичен интерфейсу Реального Субъекта.

Source from the content-addressed store, hash-verified

51
52
53class Proxy(Subject):
54 """
55 EN: The Proxy has an interface identical to the RealSubject.
56
57 RU: Интерфейс Заместителя идентичен интерфейсу Реального Субъекта.
58 """
59
60 def __init__(self, real_subject: RealSubject) -> None:
61 self._real_subject = real_subject
62
63 def request(self) -> None:
64 """
65 EN: The most common applications of the Proxy pattern are lazy loading,
66 caching, controlling the access, logging, etc. A Proxy can perform one
67 of these things and then, depending on the result, pass the execution to
68 the same method in a linked RealSubject object.
69
70 RU: Наиболее распространёнными областями применения паттерна Заместитель
71 являются ленивая загрузка, кэширование, контроль доступа, ведение
72 журнала и т.д. Заместитель может выполнить одну из этих задач, а затем,
73 в зависимости от результата, передать выполнение одноимённому методу в
74 связанном объекте класса Реального Субъекта.
75 """
76
77 if self.check_access():
78 self._real_subject.request()
79 self.log_access()
80
81 def check_access(self) -> bool:
82 print("Proxy: Checking access prior to firing a real request.")
83 return True
84
85 def log_access(self) -> None:
86 print("Proxy: Logging the time of request.", end="")
87
88
89def client_code(subject: Subject) -> None:

Callers 1

main.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected