代理模式之动态代理

package com.jason.pattern.proxy.dynamic;

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

/**
 * 动态代理
 * @author Administrator
 *
 */
public class DynamicHandler implements InvocationHandler
{
 private Object obj;// 代理的任何对象的引用

 public DynamicHandler(Object obj)
 {
  this.obj = obj;
 }
 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable
 {
  System.out.println("before daynamic ......" + method);  
  method.invoke(obj, args);// args是方法中传递的参数  
  System.out.println("after daynamic ......" + method);
  return null;
 }
}

///

package com.jason.pattern.proxy.dynamic;

/**
 * 动态代理
 * @author Administrator
 *
 */
public interface ISubject
{
 void request();
}

//

package com.jason.pattern.proxy.dynamic;

public class RealSubject implements ISubject
{

 @Override
 public void request()
 {
  System.out.println("From real subject");
 }

}

package com.jason.pattern.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class Client
{
 /**
  * @param args
  */
 public static void main(String[] args)
 {
  RealSubject realSubject = new RealSubject();
  InvocationHandler handler = new DynamicHandler(realSubject);
  Class<?> classType = handler.getClass();
  ISubject sub = (ISubject) Proxy.newProxyInstance(classType
    .getClassLoader(), realSubject.getClass().getInterfaces(),
    handler);
  sub.request();
  System.out.println(sub.getClass());
 }

}

 

//下面看另外一个例子

package com.jason.pattern.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Vector;

public class VectorProxy implements InvocationHandler
{
 private Object object;

 public VectorProxy(Object object)
 {
  this.object = object;
 }

 public static Object factory(Object obj)
 {
  return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
    .getClass().getInterfaces(), new VectorProxy(obj));
 }

 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable
 {
  System.out.println("before calling:" + method);
  if (args != null)
  {
   for (Object obj : args)
    System.out.println(obj);// 输出传递的参数
  }
  Object o = method.invoke(object, args);
  System.out.println("after calling:" + method);
  return o;
 }

 // test
 public static void main(String[] args)
 {
  List v =(List) factory(new Vector());
  v.add("Hello");
  v.add("World");
  System.out.println(v);
 }
}

///输出:before calling:public abstract boolean java.util.List.add(java.lang.Object)
Hello
after calling:public abstract boolean java.util.List.add(java.lang.Object)
before calling:public abstract boolean java.util.List.add(java.lang.Object)
World
after calling:public abstract boolean java.util.List.add(java.lang.Object)
before calling:public java.lang.String java.lang.Object.toString()
after calling:public java.lang.String java.lang.Object.toString()
[Hello, World]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值