一.静态代理
新建一个接口 定义一个玩家方法:
package com."".proxy.staticc; public interface Iplayer { public void play(); }
建一个你自己玩游戏的类
package com."".proxy.staticc; public class YourPlayer implements Iplayer { public void play() { System.out.println("登录"); System.out.println("刷图中======"); System.out.println("刷图中======"); System.out.println("刷图中======"); System.out.println("退出"); } }
建立代理类
package com."".proxy.staticc; public class DaLiPlayer implements Iplayer{ private Iplayer player = new YourPlayer(); public DaLiPlayer(Iplayer player){ this.player = player; } //环绕通知 public void play() { System.out.println("代练");//前置通知 player.play(); System.out.println("还号");//后置通知 } }
开始代理玩游戏
package com."".proxy.staticc; public class Client { public static void main(String[] args) { //传递玩家账号 YourPlayer yourPlayer = new YourPlayer(); //找到代理人 DaLiPlayer daLiPlayer = new DaLiPlayer(yourPlayer); //开始玩游戏 daLiPlayer.play(); } }
二.动态代理
动态代理的方法和静态比较类似 只是可以动态的代理目标 这里展示下具体代码
package com."".jdkproxy; public interface Iplayer { String name = "123"; public abstract void play(); }
package com."".jdkproxy; public class DnfPlayer implements Iplayer { public void play() { System.out.println("我只玩DNF,把你们负责人叫出来!"); } }
package com."".jdkproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyPlay implements InvocationHandler{ private Object targetObject = new Object(); public Object createProxy(Object targetObject){ this.targetObject = targetObject; return Proxy.newProxyInstance( targetObject.getClass().getClassLoader(), targetObject.getClass().getInterfaces(), this); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("前置代理"); Object obj = method.invoke(targetObject, args); System.out.println("后置代理"); return obj; } }
package com."".jdkproxy; public class Test { public static void main(String[] args) { //获得需求的玩家 Iplayer player = new DnfPlayer(); //找对应的代理对象 ProxyPlay proxy = new ProxyPlay(); //执行代练方法 Iplayer pl = (Iplayer) proxy.createProxy(player); pl.play(); //魔兽玩家 WowPlayer wow = new WowPlayer(); Iplayer pl2 = (Iplayer) proxy.createProxy(wow); pl2.play(); } }
三.Cglib代理详解
package com."".cglibproxy; public interface IPlayer { public void play(); }
package com."".cglibproxy; public class DNFPlayer implements IPlayer{ public void play() { System.out.println("我只玩DNF 给我找个代练"); } }
package com."".cglibproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; //cglib代理 public class ProxyHome implements MethodInterceptor{ private Object tarObject; public Object createCglib(Object tarObject){ this.tarObject = tarObject; Enhancer enhancer = new Enhancer(); enhancer.setClassLoader(tarObject.getClass().getClassLoader()); enhancer.setInterfaces(tarObject.getClass().getInterfaces()); enhancer.setCallback(this); return enhancer.create(); } public Object intercept(Object obj, Method method, Object[] args, MethodProxy arg3) throws Throwable { System.out.println("给你登录代练开始"); Object object = method.invoke(tarObject, args); System.out.println("给你退出代练结束"); return object; } }
package com."".cglibproxy; public class Test { public static void main(String[] args) { IPlayer p2 = new DNFPlayer(); ProxyHome proxy = new ProxyHome(); IPlayer tp = (IPlayer) proxy.createCglib(p2); tp.play(); } }