How to use proxy technology in Java program development

Source: Internet
Author: User
Tags array implement
Program

If you need to create a class that implements a given set of interfaces at run time, and note that this class was created at runtime, you need to use the proxy class in Java to achieve your requirements.

The proxy class provides the ability to create classes at run time, and the new class implements the interface you specify. Of course, you cannot define new code for the methods in the interface to implement at run time, but instead provide a call processor to implement. The calling processor is an object of the class that implements the Invocationhandler interface, and this interface has only one method:

Object Invoke (Object Proxy,method mtd,object[] args)

Where proxy is the proxy object, MTD is the object of the class that describes the method, which executes its invoke (object obj,object[] args) method to invoke the method encapsulated in the Mthod object; args is an array of the method arguments stored.

To establish a proxy object: Use the Newproxyinstance method of the proxy class, in the following format:

Object proxy=proxy.newproxyinstance (Null,interfaces,handler);

Where the first parameter is a class loader, this uses the default loader, represented by NULL, and the second parameter is an array of class, each of which is an interface, which is used to specify the set of interfaces to implement, even if only one interface is passed through an array, and the third parameter is the calling processor.

When a method is invoked on a proxy object, the Invoke method of the calling processor is automatically invoked, the method and parameter being invoked are automatically passed to the Jmtdt args form parameter of the Inboke method, and the method specified in the MTD object is invoked with the arguments in the args parameter array.

Agent Application: Method invocation Tracking (wrapping objects to be tracked into proxy class objects); Providing routes for remote invocation methods; User interface events are associated with actions (similar functionality in C #).



Related Article

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.