java 代理模式(静态、jdk动态代理)

本次小案例,目标类返回的结果是string类型,值为hello world,代理类的增强效果是将目标类的结果转换为大写。。

一、静态代理

1、目标类的接口

package statics.proxy;

/**
 * 目标类的接口
 *
 * @author LZJ
 * @create 2018-07-17 21:44
 **/
public interface DoInterface {
     String doSomeThing();
}

2、目标类

package statics.proxy;

/**
 * 目标类
 *
 * @author LZJ
 * @create 2018-07-17 21:45
 **/
public class DoInterfaceImpl implements DoInterface {
    public String doSomeThing() {
        return "hello world";
    }
}

3、代理类

package statics.proxy;

/**
 * 代理类
 *
 * @author LZJ
 * @create 2018-07-17 21:48
 **/
public class ProxyClass implements DoInterface{
    DoInterface doInterface = new DoInterfaceImpl();
    public String doSomeThing() {
        return doInterface.doSomeThing().toUpperCase();
    }
}

4、测试类

package statics.proxy;

/**
 * @author LZJ
 * @create 2018-07-17 21:55
 **/
public class Test {
    public static void main(String[] args){
        System.out.println(new DoInterfaceImpl().doSomeThing());

        DoInterface proxy = new ProxyClass();
        String s = proxy.doSomeThing();
        System.out.println(s);
    }
}

5、结果:

二、动态代理之 jdk的 Proxy方式

1、目标类的接口

package dynamic.proxy;

/**
 * 目标类的接口
 *
 * @author LZJ
 * @create 2018-07-17 21:44
 **/
public interface DoInterface {
     String doSomeThing();
}

2、目标类

package dynamic.proxy;

/**
 * 目标类
 *
 * @author LZJ
 * @create 2018-07-17 21:45
 **/
public class DoInterfaceImpl implements DoInterface {
    public String doSomeThing() {
        return "hello world";
    }
}

3、生成代理类的类

package dynamic.proxy;

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

/**
 * 生成代理的工厂
 *
 * @author LZJ
 * @create 2018-07-17 21:48
 **/
public class ProxyFactory {
    private static DoInterface target = new DoInterfaceImpl();

    public static DoInterface createProxy(){
        DoInterface proxyInstance = (DoInterface)Proxy.newProxyInstance(
                target.getClass().getClassLoader(),//目标类加载器
                target.getClass().getInterfaces(),//目标类实现的接口
                new InvocationHandler() {//匿名内部类
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        Object invoke = method.invoke(target, args);
                        if (invoke != null) {
                            invoke = ((String)invoke).toUpperCase();
                        }
                        return invoke;
                    }

                });
        return proxyInstance;
    }

}

4、测试类

package dynamic.proxy;

/**
 * @author LZJ
 * @create 2018-07-17 21:55
 **/
public class Test {
    public static void main(String[] args){

        System.out.println(new DoInterfaceImpl().doSomeThing());

        DoInterface proxy = ProxyFactory.createProxy();
        String s = proxy.doSomeThing();
        System.out.println(s);
    }
}

5、结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值