代理模式

本文通过具体实例介绍了Java代理模式的基本概念及应用方式,包括普通代理模式和动态代理模式,并探讨了其在屏蔽对象真实方法实现方面的用途。

学习spring 之前java中的几个模式是很重要的 其中一个就是代理模式。下面用几个简单的例子来理解一下这个模式。以及它的应用。

1.基础讲解
java.lang.reflect
提供类和接口,以获得关于类和对象的反射信息。

Proxy 提供用于创建动态代理类和实例的静态方法,它还是由这些方法创建的所有

动态代理类的超类。

java.lang.reflect.InvocationHandler 是代理实例的调用处理程序 实现的接口。

返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程

序。Proxy.newProxyInstance (ClassLoader loader,   Class<?>[] interfaces,   

InvocationHandler h)
  此方法相当于:
     Proxy.getProxyClass(loader, interfaces).
         getConstructor(new Class[] { InvocationHandler.class }).
         newInstance(new Object[] { handler });

设置class实例的代理 处理程序(ArrayListProxy()来) ;代理pro;
ArrayListProxy();是接口为InvocationHandler的类
Object pro=Proxy.newProxyInstance(class.getClassLoader(),class.getInterfaces

(),new ArrayListProxy(o));
2.实例研究
//通过代理我们可以屏蔽掉对象的真实方法的实现。来添加自己相关的业务逻辑。
2.1普通的代理模式//下面的中介商就是代理,通过中介商屏蔽了房东的出租方法.

class House{
    private float price=200f;

    public float getPrice(){
        return price;
    }
   
    public void  setPrice(){
        this.price = price;
    }
}
//房东
class  HouseOwner{
    House house = null;
   
    HouseOwner(House house){
        this.house = house;
    }   
   
    public void rent(){
        System.out.println ("房东出租房子价格是:"+house.getPrice());
    }
}


//中介商
class  HouseProxy {
    //20% 的中介费
    float centPricePoint = 0.2f;
   
    HouseOwner own = null;
   
   
    HouseProxy (HouseOwner own){
         this.own = own;
    }
   
    public void rent(){
       
        //调用真实的方法前做一些事
        System.out.println ("收取中介费"+own.house.getPrice()*centPricePoint);
       
        own.rent();
       
        //调用真实的方法后做一些事
        System.out.println ("交易成功。");
   
    }
   
   
   
}
//客户
class Rentor {
    public static void main(String[] args){
        House h = new House();
       
        //客户找房东
        HouseOwner o = new HouseOwner(h);
        o.rent();
       
       
        System.out.println ("*******************");
       
        //客户找代理
        HouseProxy hp = new HouseProxy(o);
        hp.rent();
    }
}

2.2动态代理模式。
//假设做一个ArrayList 的 添加提示的功能。 就是 在调用它的add方法的时候 输出一句 请检查类型.
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.*;
import java.util.*;
class ArrayListProxy implements  InvocationHandler{
    Object list ;
   
    ArrayListProxy(Object list ){
        this.list  = list;
    }
   
   
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable{
       
        if(method.getName().equals("add")){
            //对特定方法代理
            System.out.println ("请检查类型.befor:"+method.getName());
        }
       
        //对所有方法代理
        //System.out.println ("请检查类型.befor:"+method.getName());
        Object o =  method.invoke(list,args);
       
        return o ;
       
    }
   
   
    public static Object factory(Object o ){
        Class c = o.getClass();
       
        //用真实的对象来构造代理对象
        return Proxy.newProxyInstance(c.getClassLoader(),c.getInterfaces(),new ArrayListProxy(o));
    }
}

class TestDProxy{
    public static void main(String[] args){
        ArrayList al = new ArrayList();
        List pro =  (List)ArrayListProxy.factory(al);
       
       
        pro.add("aaa");
        System.out.println (pro.size());
       
       
       
    }
}
通过代理我们屏蔽了真实对象的实现 。理解这个模式后才能更好的理解 AOP

http://blog.youkuaiyun.com/caoyinghui1986/archive/2008/05/16/2450221.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值