English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Master Java Proxy Pattern Easily

Let's talk about the Java proxy pattern together

1, static proxy

1.1Both the proxy class of static proxy and the class being proxied need to maintain a common interface. 

public interface IUserDao {
  void save();
} 

1.2The class being proxied, the target object 

public class UserDao implements IUserDao{
  @Override
  public void save() {
    System.out.println("-----Data has been saved!!!------");
  }
} 

1.3Proxy object 

public class UserDaoProxy implements IUserDao{
  // Receive the saved target object
  private IUserDao target;
  public UserDaoProxy(IUserDao target) {
    this.target = target;
  }
  @Override
  public void save() {
    System.out.println("Start transaction...");
    target.save();       // Execute the method of the target object
    System.out.println("Submit transaction...");
  }
}

1.4Test class 

public class App {
  public static void main(String[] args) {
    // Target object
    IUserDao target = new UserDao();
    // Proxy
    IUserDao proxy = new UserDaoProxy(target);
    proxy.save(); // It executes is, the proxy method
  }
}

2, dynamic proxy

2.1Similarly, dynamic proxy also needs to complete an interface. (As above)

2.2The target object is also the same.

2.3It is just different on the proxy object 

public class ProxyFactory {
  // Maintain a target object
  private Object target;
  public ProxyFactory(Object target){
    this.target = target;
  }
  // Generate proxy object for target object 
  public Object getProxyInstance() {
    return Proxy.newProxyInstance(
        target.getClass().getClassLoader(), 
        target.getClass().getInterfaces(),
        new InvocationHandler() {
          @Override
          public Object invoke(Object proxy, Method method, Object[] args)
              throws Throwable {
            System.out.println("Start transaction");
            // Execute target object method
            Object returnValue = method.invoke(target, args);  
            System.out.println("Commit transaction");
            return returnValue;
          }
        });
  }
}

2.4Test class 

public class App {
  public static void main(String[] args) {
    // Target object
    IUserDao target = new UserDao();
    System.out.println(target.getClass());
    // Create proxy object for target object
    IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance();
    System.out.println(proxy.getClass());
    // Execute method  【proxy object】
    proxy.save();
  }
}

3, and cglib proxy

3.1cglib proxy does not need to complete the interface, just write the class to be proxied and the proxy class, here the class to be proxied is the same1.2, so it is no longer written.

3.2The proxy class is different. To use the cglib proxy pattern, you need to reference the core framework package of spring. 

public class ProxyFactory implements MethodInterceptor{
  // Maintain target object
  private Object target;
  public ProxyFactory(Object target){
    this.target = target;
  }
  // Create proxy object for target object
  public Object getProxyInstance(){
    //1. Utility class
    Enhancer en = new Enhancer();
    //2. Set superclass
    en.setSuperclass(target.getClass());
    //3. Set callback function
    en.setCallback(this);
    //4. Create subclass (proxy object)
    return en.create();
  }
  @Override
  public Object intercept(Object obj, Method method, Object[] args,
      MethodProxy proxy) throws Throwable {}}
    System.out.println("Start transaction.....");
    // Execute the method of the target object
    Object returnValue = method.invoke(target, args);
    System.out.println("Submit transaction.....");
    return returnValue;
  }
}

3.3Test class 

public class App {
  public static void main(String[] args) {
    // Target object
    UserDao target = new UserDao();
    System.out.println(target.getClass());
    // Proxy object
    UserDao proxy = (UserDao) new ProxyFactory(target).getProxyInstance();
    System.out.println(proxy.getClass());
    // Execute the method of the proxy object
    proxy.save();
  }
}

That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like