Docs

Design Patterns — Fast Recall (Python)

Proxy — a stand-in that controls access


  • What: a placeholder with the same interface as the real object, sitting in front of it.
  • Why / when: you need to add a gate before the real work — lazy loading, access control, caching, rate-limiting, remote calls.
  • Flavors: *virtual* (create on first use), *protection* (check permissions), *caching*, *remote*.
  • vs Decorator: Decorator adds features; Proxy controls/guards access (often creates the real object itself).
1class RealImage: 2 def __init__(self, f): print("loading", f); self.f = f 3 def show(self): print("show", self.f) 4 5class LazyImage: # proxy: defer the heavy load 6 def __init__(self, f): self.f, self.real = f, None 7 def show(self): 8 if self.real is None: # create only when needed 9 self.real = RealImage(self.f) 10 self.real.show() 11 12img = LazyImage("cat.png") # nothing loaded yet 13img.show() # loads, then shows