Proxy Pattern

Tak Yu Chan (Franky)
2 min readApr 8, 2021
Photo by Raimond Klavins on Unsplash

Intention

Proxy Layer for Caching subject, prevent unnecessary running of expensive subject and prevent invalid access to the subject.

The proxy has the same interface as the subject, the subject responsibility will not be affected and the Client code too.

Intro

Proxy Pattern suggests adding a proxy between the client and a subject, and the proxy needs to have the same API as the subject, it will also have some additional logic around access before executing the subject such as security, validation etc.

NOTE that the proxy doesn’t change the interface for the client but instead it controls the access and resources passed by the Client and do some thing with it then call the subject.

The Client doesn’t need to know anything in the proxy, so any changes in it will not affect itself.

Client — — → (Proxy )— — — → Subject

Features

  • Same API as Subject
  • Allow adding caching / access control etc. that is related to the subject
  • Keep Client Dumb, hide additional behavior (related to subject) from the client

There are 3 types of proxy, remote, virtual, and protection, they are both relying on the resources passed by the client but have different intents.

  • Remote: Do Caching (cache the call to object) etc. to improve performance
  • Virtual: Assume Subject is an expensive action, so we want to evaluate should we execute the action. (lazy initiation)
  • Protect: The subject is a secured action that we need to check the access right before firing it off.

--

--