静态代理:自己创建代理类生成源代码再对其编译。在程序运行前代理类的.class文件就已经存在了。
动态代理:
- 自动:可以根据我们的真实对象接口,自动生成一个增强型代码,而不要手动创建代理类写增强逻辑
- 运行时:可以在代码运行时,生成这个代理类,而不需要事先把这个类写好
废话少说,直接上代码
代码设计原则:
静态代理实现:
一、创建接口
1 public interface IGamePlayer { 2 3 //登录 4 public void login(String username ,String password); 5 //打怪 6 public void killboss(); 7 //升级 8 public void upgrade(); 9 }
二、创建实现类
1 public class GamePlayer implements IGamePlayer{ 2 3 private String name; 4 5 public GamePlayer(String name) { 6 this.name = name ; 7 } 8 9 @Override 10 public void login(String username ,String password) { 11 // TODO Auto-generated method stub 12 System.out.println(username+":登录成功"); 13 } 14 15 @Override 16 public void killboss() { 17 // TODO Auto-generated method stub 18 System.out.println(this.name+"正在打怪"); 19 } 20 21 @Override 22 public void upgrade() { 23 // TODO Auto-generated method stub 24 System.out.println(this.name+"已升级"); 25 } 26 27 }
三、创建静态代理类
1 public class GamePlayerProxy implements IGamePlayer{ 2 3 private IGamePlayer player; 4 5 public GamePlayerProxy(IGamePlayer player) { 6 // TODO Auto-generated constructor stub 7 this.player = player; 8 } 9 10 @Override 11 public void login(String username, String password) { 12 // TODO Auto-generated method stub 13 this.player.login(username, password); 14 } 15 16 @Override 17 public void killboss() { 18 // TODO Auto-generated method stub 19 this.player.killboss(); 20 } 21 22 @Override 23 public void upgrade() { 24 // TODO Auto-generated method stub 25 this.player.upgrade(); 26 } 27 28 }
测试输出:
1 public class GameTest { 2 public static void main(String[] args) { 3 4 //静态代理 5 IGamePlayer player = new GamePlayer("龙大侠"); 6 IGamePlayer proxy = new GamePlayerProxy(player); 7 proxy.login("long", "123456"); 8 proxy.killboss(); 9 proxy.upgrade(); 10 11 } 12 }
输出结果:
动态代理实现:
四、创建动态代理类
1 import java.lang.reflect.InvocationHandler; 2 import java.lang.reflect.Method; 3 4 public class DynamicProxyHandler implements InvocationHandler{ 5 6 private IGamePlayer player; 7 8 public DynamicProxyHandler(IGamePlayer player) { 9 this.player = player; 10 } 11 12 @Override 13 public Object invoke(Object proxy, Method method, Object[] args) 14 throws Throwable { 15 if(method.getName().equals("killboss")){ 16 System.out.println("戴上戒指打怪……"); 17 } 18 19 return method.invoke(player, args); 20 } 21 22 }
测试输出
1 import java.lang.reflect.InvocationHandler; 2 import java.lang.reflect.Proxy; 3 4 public class GameTest { 5 public static void main(String[] args) { 6 7 //静态代理 8 // IGamePlayer player = new GamePlayer("龙大侠"); 9 // IGamePlayer proxy = new GamePlayerProxy(player); 10 // proxy.login("long", "123456"); 11 // proxy.killboss(); 12 // proxy.upgrade(); 13 14 15 //动态代理 16 IGamePlayer player = new GamePlayer("龙大侠"); 17 18 //定义触发管理器 19 InvocationHandler hander = new DynamicProxyHandler(player); 20 21 //创建动态代理对象(三个参数:loader类加载器:把Java字节码文件加载到JVM里 。Class:被代理对象实现的所有接口。实现了触发管理器InvocationHandler的这个类 22 IGamePlayer proxy = (IGamePlayer) Proxy.newProxyInstance(player.getClass().getClassLoader(), new Class[]{IGamePlayer.class}, hander); 23 proxy.login("long", "123456"); 24 proxy.killboss(); 25 proxy.upgrade(); 26 } 27 }
输出结构:
原文地址:https://www.cnblogs.com/chai-blogs/p/12587850.html
时间: 2024-11-02 00:57:22