Proxy Design Pattern [Structural]
Tags: blog, concepts, designpattern, designpatterns
Published: 2019-03-06 03:27:39

---
title: Proxy Design Pattern [Structural]
published: true
tags: blog,concepts,Design Pattern,Design Patterns
canonical_url: https://itscoderslife.wordpress.com/2019/02/28/proxy-design-pattern-structural/
---

The core idea behind the proxy design pattern is controlling access to objects. We define the placeholder or proxy object which represents another object or a remote service. Clients use this proxy, not the real object. The proxy forwards the client request to the actual subject as necessary.

- The proxy pattern acts as a surrogate for another object and controls access to it.
- The proxy may also perform optimizations, defer the instantiation of the underlying object, or protect the resource from unauthorized access.
- We can use proxies in the following situations:
  - Remote proxies allow access to remote resources like RESTful services. The remote proxy acts as a local placeholder for a remote object. Clients can call methods of this local representative, which in turn forwards the request to the remote object.
  - Virtual proxies can be used to manage the creation of expensive resources and the execution of computationally intensive operations. We can improve performance and memory usage by postponing the costly operations until they are actually needed.
  - Protective proxies control access to sensitive objects. The surrogate object checks whether the caller has the appropriate permissions required to use the protected resource.

> The proxy pattern provides a placeholder for another object to control the way the underlying resource is accessed. The proxy introduces an additional level of interaction to support controlled, remote, or delayed access.

Example: Consider if you have to fetch and display data from network only when user does a particular action. Create a NetworkDataProxy class object set the URL and completion handler. This by itself should not download / fetch the remote content. Only when the particular user action is trigger the proxy object will fetch. This defers the downloading of content only on need basis.

Same can be done with an ImageProxy class which can be used to download image only on demand rather than pre-downloading before knowing the user needs.

Useful scenarios:

- Allow access to remote resources like RESTful services. The remote proxy acts as a local placeholder for a remote object. Clients can call methods of this local representative which in turn forwards the request to the remote object.
- Manage the creation of expensive resources and the execution of computationally intensive operations.
- We can improve performance and memory usage by postponing the creation of expensive resources until they are first accessed.
- Control access to sensitive objects. The surrogate object checks whether the caller has the appropriate permissions required to use the protected resource.

_You should not use the proxy pattern if none of these situations_ apply.

Disadvantages:

The proxy object should be used to perform all the required operations on the resource it represents. The benefits of the proxy pattern are rendered useless if clients can bypass the surrogate and use the underlying types directly.