Proxy: Introduction

Get a brief introduction to the Proxy design pattern.

Proxy is a structural pattern that allows us to provide a substitute for an object. The substitute object, known as Proxy, can perform some action before or after the original object is accessed. For example, we can cache the results of a computationally expensive operation so they can just be retrieved quickly from the cache, and the original operation doesn’t have to run again. Alternatively, if our application is connected to a network and the connectivity is lost, the Proxy object can provide substitute functionality that can work offline.

Summarized concept of the Proxy design pattern

The Proxy design pattern can be summarized as follows:

  • There’s a Main Service object that’s computationally expensive to run.
  • There’s a Proxy object that implements exactly the same interface as the Main Service.
  • All requests to Main Service are performed via the Proxy object.
  • Proxy only runs the expensive operation on Main Service when it needs to, and then it caches the results.
  • Under any other circumstances, the Proxy object returns the results from its cache without contacting Main Service.

We can understand the concept of the Proxy design pattern better through the following illustration:

Get hands-on with 1200+ tech skills courses.