加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java动态代理

发布时间:2020-12-14 06:38:07 所属栏目:Java 来源:网络整理
导读:p style="clear:both;min-height:1em;color:rgb(62,62,62);font-family:'Helvetica Neue',Helvetica,'Hiragino Sans GB','Microsoft YaHei',Arial,sans-serif;font-size:18px;" span style="color:rgb(123,12,0);"1. 什么是代理 p style="clear:both;min-hei

<p style="clear:both;min-height:1em;color:rgb(62,62,62);font-family:'Helvetica Neue',Helvetica,'Hiragino Sans GB','Microsoft YaHei',Arial,sans-serif;font-size:18px;">
<span style="color:rgb(123,12,0);">1. 什么是代理


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">

/**

  • Created by cxh on 17/3/26.
  • 微商和委托方的公共接口
    */
    public interface Sell {
    void sell();
    void add();
    }

<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">
Vendor类的定义如下:


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">



/**

  • Created by cxh on 17/3/26.
  • 委托方实现接口方法
    */
    public class Vender implements Sell{
    @Override
    public void sell(){
    System.out.println("in sell method");
    }
    @Override
    public void add(){
    System.out.println("in add method");
    }
    }

<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">
代理类BusinessAgent的定义如下:


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">



/**

  • Created by cxh on 17/3/26.

  • 代理类实现接口sell方法
    */
    public class BusinessAgent implements Sell{
    private Vender vender;

    public BusinessAgent(Vender v){
    this.vender=v;
    }
    @Override
    public void sell(){
    vender.sell();
    }
    @Override
    public void add(){
    vender.add();
    }
    }


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">
从BusinessAgent类的定义我们可以了解到,静态代理可以通过聚合来实现,让代理类持有一个委托类的引用即可。


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">
下面我们考虑一下这个需求:给Vendor类增加一个过滤功能,只卖货给大学生。通过静态代理,我们无需修改Vendor类的代码就可以实现,只需在BusinessAgent类中的sell方法中添加一个判断即可如下所示:


<p style="clear:both;min-height:1em;color:rgb(62,sans-serif;font-size:18px;">

java.lang.reflect.InvocationHandler接口的类;一个测试类MainTest。





/**

  • Created by cxh on 17/3/26.
  • 委托方/代理 共同大家的接口
    */
    public interface Sell {
    void sell();
    void add();
    }

<pre style="font-family:Menlo;"><span style="font-size:14px;">4.2 vender类,委托方代码如下:

<pre style="font-family:Menlo;"><span style="font-size:14px;">


/**

  • Created by cxh on 17/3/26.
  • 委托方,供应商
    */
    public class Vender implements Sell {
    public void sell(){
    System.out.println("this is sell method");
    }
    public void add(){
    System.out.println("this is add method");
    }
    }

<pre style="font-family:Menlo;"><span style="font-size:14px;">4.3 代理类<span style="color:rgb(62,sans-serif;font-size:18px;">DynamicProxy代码如下:
<pre style="font-family:Menlo;"><span style="font-size:14px;"><span style="color:rgb(62,sans-serif;font-size:18px;">


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**

  • Created by cxh on 17/3/26.
  • 中介类,调用处理器
    */
    public class DynamicProxy implements InvocationHandler {
    private Object obj;//obj为动态代理对象
    public DynamicProxy(Object obje){this.obj=obje;}
    //参数说明:object为代理方对象,method调用的代理方的方法,args为调用的代理方方法的参数
    @Override
    public Object invoke(Object object,Object[] args) throws Throwable{
    System.out.println("before");
    Object result=method.invoke(obj,args);
    System.out.println("after");
    return result;
    }
    }

<pre style="font-family:Menlo;"><span style="font-size:14px;"><span style="color:rgb(62,sans-serif;font-size:18px;">4.4 测试类MainTest代码如下:
<pre style="font-family:Menlo;"><span style="font-size:14px;"><span style="color:rgb(62,sans-serif;font-size:18px;">


import java.lang.reflect.Proxy;

/**

  • Created by cxh on 17/3/26.
    */
    public class MainTest {
    public static void main(String[] args) {
    //创建中介类实类
    DynamicProxy dynamicProxy=new DynamicProxy(new Vender());

     //获取代理类实例sell
     //在下代码中,我们调用Proxy类的newProxyInstance方法来获取一个代理类实例。
     //          这个代理类实现了我们指定的接口并且会把方法调用分发到指定的调用处理器。
     Sell sell=(Sell)(Proxy.newProxyInstance(Sell.class.getClassLoader(),new Class[]{Sell.class},dynamicProxy));
     sell.add();
     sell.sell();

    }
    }


<pre style="font-family:Menlo;"><span style="font-size:14px;"><span style="color:rgb(62,sans-serif;font-size:18px;">4.5 运行结果如下:
<pre style="font-family:Menlo;"><span style="font-size:14px;"><span style="color:rgb(62,sans-serif;font-size:18px;">



Process finished with exit code 0

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读