/** * Abstract on roles, public external methods for defining proxy roles and real roles * Time: April 4, 2015 18:45:00 * In this example, abstract roles play a role in standardizing the role of proxies and real roles. * There are methods, interviews, signing contracts to receive prepayment, booking tickets, singing, closing paragraph */package com.bjsxt.cn.proxy.dynamicProxy;
public interface Star {void confer (); void Signcontract (); void Bookticket (); void sing (); void Collectmoney ();}
/** * Corresponds to a real role, implements an abstract role, defines the business logic to be implemented by the real role for use by the proxy role * that is, the object of this class (for example, Jay Chou), he wants to implement his Sing method, which is the business logic of his drug concern. This implementation of the * Sing method is intended for use by the agent role * * only to pay attention to the real business logic, other methods are not concerned about the */package com.bjsxt.cn.proxy.dynamicProxy;
public class Realstar implements Star {
/** * Interview * * * @Override public void Confer () {System.out.println ("Realstar.confer ()");}
@Override public void Signcontract () {System.out.println ("realstar.signcontract ()"); }
@Override public void Bookticket () {System.out.println ("Realstar.bookticket ()");}
@Override public void Sing () {System.out.println ("Realstar (Real role: Jay Chou). Sing ()");
}
@Override public void Collectmoney () {System.out.print ("Realstar.collectmoney ()"); } }
/** * Processor Interface Invocationhandler, you can use invoke to achieve the real role of access * Every time the proxy class object generated through proxy to specify the corresponding Processor object * */package Com.bjsxt.cn.proxy.dynamicProxy;
Import Java.lang.reflect.invocationhandler;import Java.lang.reflect.Method;
[email protected] * */public class Starhandler implements Invocationhandler {
Star Realstar = null; Public Starhandler (Star Realstar) {super (); This.realstar = Realstar; }
/** * Processing a method call on a proxy instance and returning the result, calling the method on the proxy instance of the method association, calling this method on the call * Handler * @param method * Corresponds to the method instance used to invoke the interface methods on the proxy instance. The Life class of the method object will be where the method *   is declared, and the interface can be a hyper-interface to the proxy interface on which the proxy class inherits the method. * * @param args contains an array of objects for the parameter values of the method calls on the incoming proxy instance. If the interface method does not use parameters, it is null * Note: The parameters of the base type are wrapped in an instance of the appropriate base wrapper class (Java.lang.Integer or Java.lang.Boolean) * * @param proxy instance on which the method is invoked * * @return * The value returned from the proxy instance's method call. If the declaration return type of the interface method is the base type, * then the value returned by this method must be an instance of the corresponding basic wrapper object class; * Otherwise, it must be a type that can be assigned to the declaration return type. * If this method returns a value of NULL and the return type of the interface method is the base type, * then the method call on the proxy instance will throw nullpointerexception. * Otherwise, if the value returned by this method is incompatible with the declared return type of the interface method above, * the method call on the proxy instance will be thrown classcastexception */ @Override public Object Invoke (Object proxy, Method method, object[] args) throws throwable { object Object =null; &nbSp; if (Method.getname (). Equals ("Sing")) { system.out.println ("interview, sign contract, receive prepayment, system.out.println ("Before a real method call"); object = Method.invoke (Realstar, args) ; system.out.println ("After a real method call"); system.out.println ("Closure"); } return object; }
}
/** * test Dynamic proxy class * time: April 4, 2015 20:01:43 * */package com.bjsxt.cn.proxy.dynamicproxy;/** * The proxy class provides a static method for creating dynamic proxy classes and instances, or a superclass of all dynamic proxy classes created by these methods * the * dynamic proxy class is a class that implements the interface column that is specified at run time when the class is created, which has the following behavior: Agent interface * Refers to an excuse for the implementation of a proxy class. The proxy instance is an instance of the proxy class. The method call on the proxy instance of each proxy class will * the Invoke method of the calling handler that is assigned to the instance. And the bottom of the bed proxy instance, identify the Java.lang.reflect.Method object that invokes the method and the * array containing the type of object. The calling handler handles the encoded method call in the appropriate manner. And the result it returns is returned as the result of the method call on the proxy instance. * * * generally creates a proxy for Foo of an interface: * invocationhandler handler = new Myinvocationhandler (...); /Get a processor Class Proxyclass = Proxy.getproxyclass ( Foo.class.getClassLoader (), new class[] {foo.class}); Foo f = (foo) proxyclass. &nb sp; GetConstructor (New class[] {invocationhandler.class}) . newinstance (new object[] {handler}); or use the following simpler method: (This example also uses the following method) foo f = (Foo) Proxy.newproxyinstance (Foo.class.getClassLoader (), New class[] {Foo.class}, handler);
*/import Java.lang.reflect.Proxy;
public class Client {public static void main (string[] args) {Star Realstar = new Realstar (); Starhandler handler = new Starhandler (Realstar); /** * In other words, the proxy class instance proxy, implements the specified interface list of classes (we also implemented the same star in the static proxy *, because the Star interface is implemented, so we can declare the star type, which is probably the use of a polymorphic behavior.) ) * * In this line of code, Star.class represents the proxy interface (an interface implemented by the proxy class). The proxy instance refers to proxies, which implements the Star interface */Star proxy = (star) proxy.newproxyinstance (Classloader.getsystemclassloader (), New class[]{ Star.class}, handler); Proxy.bookticket (); /** * In this line of code, we can get a rough three kinds of information, proxy is an agent instance, the method name is sing, the method has no parameters, * all passed to the proxy instance associated with the invocation handler. */proxy.sing (); }}
/* * * interview, sign contract, receive prepayment, book your ticket before the real method call Realstar (Real role: Jay Chou). Sing () after a real method call closing the paragraph
* */
The Classic of dynamic proxy pattern implementation of GOF23 design mode