Static proxy
Package com.iter.devbox.staticProxy;
Public interface Star {public
void Sing ();
}
Package com.iter.devbox.staticProxy;
public class Realstar implements Star {
@Override public
void Sing () {
System.out.println this.getclass ( ). GetName () + ". Sing ()");
}
Package com.iter.devbox.staticProxy;
Public class Starhandler implements star {
private star Realstar;
Public Starhandler (Star Realstar) {
super ();
This.realstar = Realstar;
}
@Override public
void Sing () {
System.out.println (This.getclass (). GetName () + ". Sing () before execution");
Realstar.sing ();
System.out.println (This.getclass (). GetName () + ". Sing () after execution");
Package com.iter.devbox.staticProxy;
public class Client {public
static void Main (string[] args) {
new Starhandler (New Realstar ()). sing ();
Dynamic Proxy
Package com.iter.devbox.dynamicProxy;
Public interface Star {public
void Sing ();
}
Package com.iter.devbox.staticProxy;
public class Realstar implements Star {
@Override public
void Sing () {
System.out.println (this.getclass (). GetName () + ". Sing ()");
}
Package com.iter.devbox.dynamicProxy;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
public class Starhandler implements Invocationhandler {
Object star;
Public Starhandler (Object star) {
super ();
This.star = Star;
}
@Override Public
object Invoke (Object proxy, Method method, object[] args) throws Throwable {
System.out.println ("Before the agent method executes");
Object obj = Method.invoke (star, args);
System.out.println ("After the agent method executes");
return obj;
}
}
Package com.iter.devbox.dynamicProxy;
Import Java.lang.reflect.Proxy;
public class Client {public
static void Main (string[] args) {
Star Realstar = new Realstar ();
Call Processor
Starhandler handler = new Starhandler (Realstar);
Generate proxy class interface using JDK Dynamic proxy
star star = (Star) proxy.newproxyinstance (Classloader.getsystemclassloader (),
new Class[]{star.class}, handler);
Star.sing ();
}