Sunday, May 31

Pattern of the Day

One pattern. Five minutes. Every day.

structuralintermediate

Proxy Pattern

Control access to another object through a surrogate

Simple explanation

A proxy is like a gatekeeper. When you want access to something valuable (like the CEO), you talk to their assistant (the proxy). The assistant controls when you get access, screens requests, and decides what goes through. Proxy pattern does exactly this for objects.

Security Guard (Access Control Proxy)

"A security guard at a building entrance acts as a proxy for the building. You can't just walk in—you show your ID to the guard first. The guard decides if you're allowed entry, logs who enters, checks for weapons, etc. The guard is a proxy controlling access to the building."

Use it when

  • Lazy loading: delay expensive object creation until actually needed
  • Access control: restrict who can use an object or what they can do

Today's question

How does Proxy differ from Decorator?

#lazy-loading#access-control#caching#remote-access