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 (); }
}