java 代理和动态代理


第三种关系成为代理,举一个例子,你需要去某公司问问这个公司的相关业务,那么前台的漂亮妹妹就可以充当这个代理,她帮你询问然后告诉你。


扯点正经的吧。。。。

例如,太空船需要一个控制模块:

//: reusing/SpaceShipControls.java
package reusing; /* Added by Eclipse.py */

public class SpaceShipControls {
  void up(int velocity) {}
  void down(int velocity) {}
  void left(int velocity) {}
  void right(int velocity) {}
  void forward(int velocity) {}
  void back(int velocity) {}
  void turboBoost() {}
} ///:~

以下是代理方法:

//: reusing/SpaceShipDelegation.java
package reusing; /* Added by Eclipse.py */

public class SpaceShipDelegation {
  private String name;
  private SpaceShipControls controls =
    new SpaceShipControls();
  public SpaceShipDelegation(String name) {
    this.name = name;
  }
  // Delegated methods:
  public void back(int velocity) {
    controls.back(velocity);
  }
  public void down(int velocity) {
    controls.down(velocity);
  }
  public void forward(int velocity) {
    controls.forward(velocity);
  }
  public void left(int velocity) {
    controls.left(velocity);
  }
  public void right(int velocity) {
    controls.right(velocity);
  }
  public void turboBoost() {
    controls.turboBoost();
  }
  public void up(int velocity) {
    controls.up(velocity);
  }
  public static void main(String[] args) {
    SpaceShipDelegation protector =
      new SpaceShipDelegation("NSEA Protector");
    protector.forward(100);
  }
} ///:~

代理的好处是不暴露你访问的对象,具有安全性。


好了,下面就说动态代理了

通过调用静态方法 Proxy.newProxyInstance()可以创建动态代理,这个方法需要三个参数

1.一个类加载器

2.一个你希望该代理实现的接口列表

3.  InvocationHandler 的实现


还有一个 invoke() 方法,该方法传递进来了代理对象,通常你会执行被代理的对象,通过Method,invoke() 将请求转发给被代理对象,并传入其他的参数。


以下是实际的例子


//: typeinfo/SelectingMethods.java

// Looking for particular methods in a dynamic proxy.
import java.lang.reflect.*;


class MethodSelector implements InvocationHandler {
  private Object proxied;
  public MethodSelector(Object proxied) {
    this.proxied = proxied;
  }
  public Object
  invoke(Object proxy, Method method, Object[] args)
  throws Throwable {
    if(method.getName().equals("interesting"))
      System.out.println("Proxy detected the interesting method");
    return method.invoke(proxied, args);
  }
}	

interface SomeMethods {
  void boring1();
  void boring2();
  void interesting(String arg);
  void boring3();
}

class Implementation implements SomeMethods {
  public void boring1() {  System.out.println("boring1"); }
  public void boring2() {  System.out.println("boring2"); }
  public void interesting(String arg) {
	  System.out.println("interesting " + arg);
  }
  public void boring3() {  System.out.println("boring3"); }
}	

class SelectingMethods {
  public static void main(String[] args) {
    SomeMethods proxy= (SomeMethods)Proxy.newProxyInstance(
      SomeMethods.class.getClassLoader(),
      new Class[]{ SomeMethods.class },
      new MethodSelector(new Implementation()));
    proxy.boring1();
    proxy.boring2();
    proxy.interesting("bonobo");
    proxy.boring3();
  }
} /* Output:
boring1
boring2
Proxy detected the interesting method
interesting bonobo
boring3
*///:~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值