Présentons d'abord ce qu'est un agent.
(Partage vidéo d'apprentissage : tutoriel vidéo Java)
Le proxy est un modèle de conception, et son idée principale est de transférer l'accès à la cible à l'objet proxy. L'avantage est que l'objet cible peut ajouter des fonctions supplémentaires via l'objet proxy sans changer le code. Il s'agit d'une idée de programmation qui ajoute des fonctions étendues via des agents sans modifier le code d'origine.
Le processus proxy est tel qu'illustré sur la figure. L'utilisateur accède à l'objet proxy, et l'objet proxy atteint l'objectif de l'utilisateur d'accéder à l'objet cible en accédant à l'objet cible
<.>
public interface IBlogService { void writeBlog(); }
public class BlogService implements IBlogService { @Override public void writeBlog() { System.out.println("i'm writing..."); } }
public class BlogStaticProxy implements IBlogService{ private IBlogService blogService; public BlogStaticProxy(IBlogService blogService) { this.blogService = blogService; } @Override public void writeBlog() { System.out.println("start writing..."); blogService.writeBlog(); System.out.println("end writing..."); } }
public class BlogStaticProxy implements IBlogService{ private IBlogService blogService; public BlogStaticProxy(IBlogService blogService) { this.blogService = blogService; } @Override public void writeBlog() { System.out.println("start writing..."); blogService.writeBlog(); System.out.println("end writing..."); } }
public class TestStaticProxy { public static void main(String[] args) { IBlogService target = new BlogService(); BlogStaticProxy proxy = new BlogStaticProxy(target); proxy.write(); } }
start writing… i’m writing… end writing…
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException { }
public class JdkBlogProxyFactory { private Object target; public JdkBlogProxyFactory(Object target) { this.target = target; } public Object newInstance() { return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), (proxy, method, args) -> { System.out.println("start writing"); Object o = method.invoke(target, args); System.out.println("end writing"); return o; }); } }
public class TestJdkProxy { public static void main(String[] args) { IBlogService target = new BlogService(); System.out.println(target.getClass()); // 给目标对象,创建代理对象 IBlogService proxy = (IBlogService) new JdkBlogProxyFactory(target).newInstance(); // class $Proxy0 内存中动态生成的代理对象 System.out.println(proxy.getClass()); // 执行方法 【代理对象】 proxy.writeBlog(); } }
class com.forezp.proxy.BlogService class com.sun.proxy.$Proxy0 start writing i'm writing... end writing
public class CglibBlogFactory implements MethodInterceptor { private Object target; public CglibBlogFactory(Object target) { this.target = target; } //给目标对象创建一个代理对象 public Object getProxyInstance() { //1.工具类 Enhancer en = new Enhancer(); //2.设置父类 en.setSuperclass(target.getClass()); //3.设置回调函数 en.setCallback(this); //4.创建子类(代理对象) return en.create(); } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("start writing..."); //执行目标对象的方法 Object returnValue = method.invoke(target, objects); System.out.println("end writing..."); return returnValue; } }
public class TestCglib { public static void main(String[] args) { IBlogService target = new BlogService(); //代理对象 IBlogService proxy = (IBlogService) new CglibBlogFactory(target).getProxyInstance(); //执行代理对象的方法 proxy.writeBlog(); } }
start writing... i'm writing... end writing...
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!