Using reflection to implement dynamic proxy

Source: Internet
Author: User

Proxy mode: Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object may not fit or directly refer to another object, and the proxy object can act as a mediator between the client and the target object.

The analysis was designed to three objects:

One: Abstract class interface

Second: By proxy class (specific implementation of the abstract interface Class)

Three: proxy class: A class that actually invokes methods and properties of the proxy class,

Dynamic proxies invoke proxy class methods and properties by creating Run-time class objects based on class classes loaded by objects in memory. The specific code example is below.

<span style= "font-family:arial, Helvetica, Sans-serif;" >//</span><span style= "Color:rgb (51, 51, 51); Font-family:arial, XXFarEastFont-Arial, Sans-serif; font-size:14px; line-height:24px; " > Abstract class Interface </span>
Interface Human {<span> </span> void info ();
void Fly (); }//represented Class class Superman implements Human {public void info () {System.out.println ("I am Superman.") I'm afraid of WHO.
	");
	public void Fly () {System.out.println ("I Believe I can fly!"); The class Myinvocationhandler implements Invocationhandler {object obj;//the declared public void setobject of the proxy class object (object ob
	j) {this.obj = obj; @Override public Object Invoke (object proxy, Method method, object[] args) throws Throwable {Object ReturnVal

		= Method.invoke (obj, args);
	return returnval; The class Myproxy {//dynamically creates a proxy class object <strong> (emphasis) public static object Getproxyinstance (Object obj) {Myinvocati
		Onhandler handler = new Myinvocationhandler ();

		Handler.setobject (obj); Return Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), obj. GetClass (). Getinterfaces (), handler); </ Strong>}} public class Testaop {public static void main (string[] args) {Superman mans = new Superman ();//Create a generation Rationale classObject obj = Myproxy.getproxyinstance (man);//Returns the object of a proxy class Human Hu = (Human) obj;
		
		Hu.info ();//System.out.println () of an abstract method overridden by an object invocation of a proxy class;
		
		
	Hu.fly (); }
}

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.