Java dynamic proxy sample sharing

  • 2020-04-01 02:57:47
  • OfStack

First, analyze the three roles in the dynamic proxy pattern:
1. Abstract role: in the static proxy, it can be an abstract class, but in the dynamic proxy, it can only be an interface
2. Real personas: this is just a way of implementing an abstract role
3. The proxy role: the most disgusting one is the one in the dynamic proxy. It holds references to real characters.

It involves an interface and a class, the InvocationHandler interface and the Proxy class. According to JDK documentation, the InvocationHandler interface is implemented by a class whose instance is a handler object corresponding to a proxy object. When a method of the proxy object is called, the method is coded and assigned to the invoke method of its handler object.


//Abstract role:
public interface AbstractRole
{
public void show();
}
//Real characters:
public class RealRole implements AbstractRole
{
@Override
public void show(){ System.out.println("show me your house"); }
}
//Acting roles:
//I think this is just a pseudo-proxy, pseudo-proxy is my own idea = =! Because it's actually the handler for the proxy
public class Handler implements InvocationHandler
{
private Object realRole; //A reference to a real role is required in the proxy role, and is common when defined as an Object type
public Handler(Object realRole)
{ this.realRole = realRole; }
@Override
public Object invoke(Object proxy, Method method, Object[] args)
{
System.out.println("Give me your money");  //This is an additional feature added by the proxy role itself
method.invoke(this.realRole, args); //Methods that invoke the real role through reflection
System.out.println("Ok...house is yours");//This is an additional feature added by the proxy role itself
}
public Object factory()//The actual proxy role is generated through the factory method
{
return Proxy.newProxyInstance(this.getClass().getClassLoader(), this.realObject.getClass().getInterfaces(), this); //The newProxyInstance method in the Proxy has two important characteristics! The first is to dynamically create a proxy class if the output appears to be a class called $Proxy0. The second is to generate an instance by dynamically creating the class.
}
}
//Client:
public class Test
{
   public static void main(String[] args)
  {
     RealRole realRole = new RealRole();//Create a real character that you want to represent
     Handler handler = new Handler(realRole);//So here we have the handler for the proxy class, which I want to call the pseudo-proxy object
     AbstractRole proxy = (AbstractRole)handler.factory();//Proxy objects are generated through factory methods
  }
}

You might wonder how the proxy role can be strongly converted to an abstract role here. The reason is the newProxyInstance method, which is too interesting ~ as mentioned above, it will automatically generate a class and then generate proxy objects through the class. This class actually implements the abstract role class. Why? Because the second parameter already specifies which interfaces it implements. So you can go strong, and then you can call methods in the abstract role

Proxy. The show (); / / good! Notice this sentence, which I marked out in red at the beginning, "this method will be coded and assigned to the invoke method of its handler object!" Because of this, proxy.show() simply passes the show method to the invoke method in the handler object, along with the show method's arguments, but the show method has no arguments. So proxy.show() actually calls the invoke method in the handler object.


Related articles: