2 ways to implement Java dynamic proxy _java

Source: Internet
Author: User
Tags throwable

The Java dynamic Proxy is described on the Java API, and is not written here. I understand the agent:

An extension of the functionality of a particular method in a particular interface, which is the proxy. An agent invokes a method by invoking the handler object associated with the proxy instance.

Here is an example to look at:

Interface:

Public interface Num {
  void Show ();
  
  int Getnum ();
  
  int getproduct (int x);
}

Implementation class:

public class MyNum implements Num {
  @Override public
  int Getnum () {return
    3;
  }
  
  @Override public
  int getproduct (int x) {return
    x;
  }

  @Override public
  Void Show () {
    System.out.println ("The underlying method prints a number of digits");
  }


Let's take a look at how the Invoke method is described in the API

This means that the calling handler invokes the underlying method of the object representation of the implementation class object of the interface.

The first way to implement a proxy:

public class Numproxy {private Object num;
  The implementation class object of the interface is constructed by construction method public Numproxy (object num) {this.num = num; The public object Getnumbyproxy () {Object numproxy = Proxy.newproxyinstance (Num.getclass (). getClassLoader (), New Clas S[]{num.class}, new Invocationhandler () {/** * method: an instance of methods that corresponds to an interface approach invoked on a proxy instance. What I understand is the actual method of being represented. * Args: I understand the real method of the parameter array/@Override public object Invoke (object proxy, Methods M
        Ethod, object[] args) throws Throwable {Object obj = null;
        System.out.println ("Start recording before Method");
        String methodname = Method.getname ();
          if ("GetProduct". Equals (methodname)) {obj = Method.invoke (num, args);
          obj = (Integer) obj * 2;
        System.out.println ("Proxy:getproduct () end");
          else if ("Show". Equals (methodname)) {obj = Method.invoke (num, args);
        System.out.println ("Proxy:show () end");
      return obj;
   }
    }); return numproxy;

 }
}

The second way to implement the proxy: by implementing the Invocationhandler interface

public class Numproxyimpl implements Invocationhandler {
//Here I materialized the interface type, not written as Object
  private num num;
  
  Public numproxyimpl (num num) {
    this.num = num;
  }
  
  @Override Public
  object Invoke (Object proxy, Method method, object[] args)
      throws Throwable {
    Object obj = nul l;
    String methodname = Method.getname ();
    if ("GetProduct". Equals (methodname)) {
      System.out.println ("proxy:getproduct () Start");
      obj = Method.invoke (num, args);
      obj = (Integer) obj * 2;
      System.out.println ("Proxy:getproduct () End");
    else if ("Show". Equals (methodname)) {
      System.out.println ("Proxy:show () Start");
      obj = Method.invoke (num, args);
      System.out.println ("Proxy:show () End");
    return obj;
  }
}

Test code:

public class Testnum {public
  static void Main (string[] args) {
//two ways to test
    numproxy NP = new Numproxy (new MyNum ());
    num numproxy = (num) np.getnumbyproxy ();
    int x = numproxy.getproduct (2);
    SYSTEM.OUT.PRINTLN (x);
    Numproxy.show ();
    
    System.out.println ("----------------");
    Numproxyimpl NPI = new Numproxyimpl (new MyNum ());
    num Numpro = (num) proxy.newproxyinstance (Num.class.getClassLoader (), New Class[]{num.class}, NPI);
    int n = numpro.getproduct (3);
    SYSTEM.OUT.PRINTLN (n);
    Numpro.show ();
  }

Console results:

The second way a little puzzled, do not know if you have no, that is, and did not show the Invoke method in the Numproxyimpl, but the implementation of the, um, well, let's go down and have a look.

Just remember that you don't want to get into trouble.

For example, code processing can be used to the agent, the next time to write an example.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.