mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 10:36:39 +00:00
* doc: add proxy README documentation * doc: edit general README files * add proxy code example Co-authored-by: Ruidy <r.nemausat@empfohlen.de>
28 lines
969 B
Python
28 lines
969 B
Python
from structural.proxy.real_subject import RealSubject
|
|
from structural.proxy.subject import Subject
|
|
|
|
|
|
class Proxy(Subject):
|
|
"""The Proxy has an interface identical to the RealSubject."""
|
|
|
|
def __init__(self, real_subject: RealSubject) -> None:
|
|
self._real_subject = real_subject
|
|
|
|
def request(self) -> None:
|
|
"""
|
|
The most common applications of the Proxy pattern are lazy loading,
|
|
caching, controlling the access, logging, etc. A Proxy can perform one
|
|
of these things and then, depending on the result, pass the execution to
|
|
the same method in a linked RealSubject object.
|
|
"""
|
|
|
|
if self.check_access():
|
|
self._real_subject.request()
|
|
self.log_access()
|
|
|
|
def check_access(self) -> bool:
|
|
print("Proxy: Checking access prior to firing a real request.")
|
|
return True
|
|
|
|
def log_access(self) -> None:
|
|
print("Proxy: Logging the time of request.", end="")
|