设计模式(六)代理模式(Proxy)

转载请标明出处:
http://blog.csdn.net/qq_34707744/article/details/79208049

本文出自:【生平少年】

这里写图片描述

定义:

为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用

组成:

抽象角色:通过接口或抽象类声明真实角色实现的业务方法。
代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用。

分类

静态代理:静态定义代理类,我们自己静态定义的代理类。
动态代理:通过程序动态生成代理类,该代理类不是我们自己定义的。而是由程序自动生成。
Cglib代理:也叫作子类代理,它是在内存中构建一个子类对象从而实现对目标对象功能的扩展. 需要引入(spring-core-3.2.5.jar)

以我们在支付宝中充话费为例

静态代理

抽象角色接口:中国移动抽象类(ChinaMobileInterface)
代理角色实现:支付宝Alipay
真实角色实现:中国移动具体类(ChinaMobile)

//抽象角色接口
interface ChinaMobileInterface{
    void PayPhoneBill();
}
//代理角色实现类(代理真实角色中的操作--充值)
class Alipay implements ChinaMobileInterface{
    private ChinaMobileInterface chinaMobileInterface;
    @Override
    public void PayPhoneBill() {
        System.out.println("支付宝开始向中国移动充话费...");
    }
    public Alipay(ChinaMobileInterface chinaMobileInterface) {//通过构造器给真实角色赋值
        this.chinaMobileInterface = chinaMobileInterface;
    }
}

//真实角色实现类
class ChinaMobile implements  ChinaMobileInterface{
    @Override
    public void PayPhoneBill() {
        System.out.println("中国移动开始充话费...");
    }
}

测试

public static void main(String[] args) {
 ChinaMobileInterface chinaMobile = new ChinaMobile();
 ChinaMobileInterface alipay = new Alipay(chinaMobile);
 alipay.PayPhoneBill();    
}

动态代理

动态代理是不需要定义代理角色的,通过一个处理器来处理代理角色的业务逻辑。

真实角色实现类

class ChinaMobile01 implements  ChinaMobileInterface{
    @Override
    public void PayPhoneBill() {
        System.out.println("中国移动开始充话费...");
    }
}

代理角色的处理器

class ChinaMobileHander implements InvocationHandler{
    private  ChinaMobileInterface chinaMobileInterface;
    /**
     * 所有的流程控制都在invoke方法中
     * proxy:代理类
     * method:正在调用的方法
     * args:方法的参数
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object object = null;
        System.out.println("真实角色调用之前的处理.....");

        if (method.getName().equals("sing")) {
            object = method.invoke(chinaMobileInterface, args);//激活调用的方法
        }
        System.out.println("真实角色调用之后的处理.....");
        return object;
    }

    public ChinaMobileHander(ChinaMobileInterface chinaMobileInterface) {
        super();
        this.chinaMobileInterface = chinaMobileInterface;
    }
}

测试

public static void main(String[] args) {
//真实角色
ChinaMobileInterface chinaMobileInterface = new ChinaMobile();
//处理器
ChinaMobileHander handler = new ChinaMobileHander(chinaMobileInterface);
//代理类
ChinaMobileInterface proxy = (ChinaMobileInterface) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{ChinaMobileInterface.class}, handler);
proxy.PayPhoneBill();  
}

Cglib代理工厂

Cglib代理,也叫作子类代理,它是在内存中构建一个子类对象从而实现对目标对象功能的扩展。JDK的动态代理有一个限制,就是使用动态代理的对象必须实现一个或多个接口,如果想代理没有实现接口的类,就可以使用Cglib实现.

public class ProxyFactory implements MethodInterceptor{
    //维护目标对象
    private Object target;
    public ProxyFactory(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 obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("开始事务...");
        //执行目标对象的方法
        Object returnValue = method.invoke(target, args);
        System.out.println("提交事务...");
        return returnValue;
    }
}

测试

//目标对象
ChinaMobileCglib target = new ChinaMobileCglib();
//代理对象
ChinaMobileCglib proxy = (ChinaMobileCglib)new ProxyFactory(target).getProxyInstance();
//执行代理对象的方法
proxy.save();

比较

内容静态代理动态代理Cglib代理
特点自己静态定义的代理类通过程序动态生成代理类,该代理类不是我们自己定义的。而是由程序自动生成,动态代理也叫做:JDK代理,接口代理在内存中构建一个子类对象从而实现对目标对象功能的扩展
优点可以做到在不修改目标对象的功能前提下,对目标功能扩展.代理对象,不需要实现接口目标对象没有实现接口,可用Cglib代理
缺点一旦接口增加方法,目标对象与代理对象都要维护代理对象不需要实现接口,但是目标对象一定要实现接口,否则不能用动态代理需要引入外包

THE END

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SokachWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值