Understanding Static and Dynamic Proxies in Java

The proxy pattern provides a indirect way to access a target object, allowing additional functionality such as pre- and post-interception to be added. This pattern is useful for extending the capabilities of the target object without modifying its implementation.

Static Proxy

A static proxy is straightforward to implement. The proxy class implements the same interface as the target object and maintains a reference to the target. The proxy clas can then intercept method calls and add additional behavior before and after invoking the target's methods.


/**
* Interface implemented by the target object
*/
public interface BusinessInterface {
   void execute();
}

/**
* Target object implementation
*/
public class Business implements BusinessInterface {
   @Override
   public void execute() {
       System.out.println("Executing business logic...");
   }
}

/**
* Static proxy class
*/
public class BusinessProxy implements BusinessInterface {
   private BusinessInterface businessImpl;

   public BusinessProxy(BusinessInterface businessImpl) {
       this.businessImpl = businessImpl;
   }

   @Override
   public void execute() {
       System.out.println("Pre-interception...");
       businessImpl.execute();
       System.out.println("Post-interception...");
   }
}
 

Summary of Static Proxy:

  • Advantages: Extends and intercepts the target object's functionality without modifying it.
  • Disadvantages: Requires the proxy class to implement the same interface as the target, leading to a large number of proxy classes and maintenance issues. Any changes to the enterface require updates to both the target and proxy classes.

Dynamic Proxy

A dynamic proxy creates a proxy object at runtime using JDK APIs. This approach does not require the proxy class to implement any interfaces, and the proxy is generated dynamically based on the target object's interfaces.


/**
* Dynamic proxy factory
*/
public class ProxyFactory {
   private Object targetObject;

   public ProxyFactory(Object targetObject) {
       this.targetObject = targetObject;
   }

   public Object getProxyInstance() {
       return java.lang.reflect.Proxy.newProxyInstance(
               targetObject.getClass().getClassLoader(),
               targetObject.getClass().getInterfaces(),
               new java.lang.reflect.InvocationHandler() {
                   @Override
                   public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
                       System.out.println("Pre-interception...");

                       Object result = method.invoke(targetObject, args);

                       System.out.println("Post-interception...");
                       return result;
                   }
               });
   }
}
 

Summary of Dynamic Proxy:

  • Advantages: No need to implement interfaces in the proxy class, reducing the number of proxy classes and simplifying maintenance. Changes to the interface do not require updating the proxy class, only the invocation handler.
  • Disadvantages: The target object must implement an interface; otherwise, JDK dynamic proxy cannot be used.

Tags: java Proxy Pattern Static Proxy Dynamic Proxy JDK

Posted on Sat, 15 Aug 2026 16:55:22 +0000 by adnan856