Skip to content
Roberto Fronteddu edited this page Feb 17, 2023 · 5 revisions

The Proxy is a Structural Design Pattern. It works by creating an interface that holds on creating/communicating with a real instance of the proxied object until necessary.

Usefull when

  • We need to transparently provide a placeholder or surrogate to another object. We may have a Protection Proxy to control access to an object. A Remote Proxy provides a local representation of a remote object. A Virtual Proxy to delay the construction of an object until necessary.

UML

proxy uml

  • Subject: Defines the interface used by the client
  • RealSubject: Provides an implementation of Subject
  • Proxy: Implements the Subject interface and maintains a reference to the real object to provide actual functionalities.

Implementation steps:

Static Proxy

Implement the proxy

  • Proxy must implement the same interface as the real subject (either create an actual object later when required or ask for one in the constructor). In the proxy, we implement the proxy functionalities before delegating to the real object.

Providing the client with the proxy instance

  • Either through a factory or compose the client code with the proxy instance.

Static and dynamic proxy

  • Proxies can be static or dynamic depending on where to not they are defined statically or at runtime.
  • Invoke method is called for all method call in the proxy, once it is invoked, we have to determine which method called it and what to do.
  • To create the proxy instance we can use java.lang.reflect.Proxy

Dynamic proxy

  • Implement java.lang.reflect.InvocationHandler, invocation handler implements the invoke method which is called to handle every method invocation on a proxy.

Design Considerations

  • Not all proxies need a reference to the real object
  • Pay attention to performance cost and synchronization issues
  • Proxies do not need to know about the concrete implementation of real objects
  • This pattern is great for implementing security or as stand-ins for real objects which may be costly objects that you want to defer loading. Proxies also make working with remote services/APIs easier by representing them as regular objects and possibly handling network comms behind the scene.

Comparison with Decorator

Proxy:

  • Does not need access to real objects all the time.
  • Its purpose is to provide features like access control, lazy loading, etc

Decorator:

  • Always need a real object to provide functionalities.
  • A decorator is meant to add functionalities to an existing functionality provided by the object and used by the client directly.

Pitfalls

  • JAVA dynamic proxy only works if your class is implementing one or more interfaces. The proxy is created by implementing these interfaces.
  • Try not to have multiple proxies manipulating the same object, it may be hard to manage their interaction

Real-Life Examples

  • Hibernates uses a proxy to load collections of value types. If you have a relationship in an entity class mapped as a collection, marked as a candidate for lazy loading, then hibernate will provide a virtual proxy in its place. Spring uses the proxy pattern to provide support for features like transactions, caching, and general AOP support.
Clone this wiki locally