Java设计模式:代理模式

2021/4/12 1:25:10

本文主要是介绍Java设计模式:代理模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

静态代理

代码结构
在这里插入图片描述
源码

package com.myspringboot.shejimoshi.daili.jingtai;

public interface Call {
    void call();
}
package com.myspringboot.shejimoshi.daili.jingtai;


import java.util.concurrent.TimeUnit;

public class Dog implements Call {
    public void call() {
        System.out.println("汪汪汪。。。");
        try {
            TimeUnit.SECONDS.sleep((int)(Math.random()*9));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
package com.myspringboot.shejimoshi.daili.jingtai;

public class DogLogProxy implements Call {
    Call call;

    public DogLogProxy(Call call) {
        this.call = call;
    }

    @Override
    public void call() {
        System.out.println("start...");
        call.call();
        long end = System.currentTimeMillis();
        System.out.println("stop!");
    }
}
package com.myspringboot.shejimoshi.daili.jingtai;

public class DogTimeProxy implements Call {
    Call call;

    public DogTimeProxy(Call call) {
        this.call = call;
    }

    @Override
    public void call() {
        long start = System.currentTimeMillis();
        call.call();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
package com.myspringboot.shejimoshi.daili.jingtai;

/**
 * 静态代理
 */
public class Main {

    public static void main(String[] args) {

        new DogLogProxy(
                new DogTimeProxy(
                        new Dog()
                )
        ).call();
    }
}

动态代理
jdk(asm)

代码结构
在这里插入图片描述
源码

package com.myspringboot.shejimoshi.daili.dongtai.jdk;

public interface Call {
    void call();
}
package com.myspringboot.shejimoshi.daili.dongtai.jdk;


public class Dog implements Call {
    public void call() {
        System.out.println("汪汪汪。。。");
    }
}
package com.myspringboot.shejimoshi.daili.dongtai.jdk;

import java.lang.reflect.Proxy;

/**
 * 使用jdk的动态代理
 */
public class Main {
    public static void main(String[] args) {
        // System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles","true");
        Dog dog = new Dog();
        Call call = (Call) Proxy.newProxyInstance(dog.getClass().getClassLoader(), new Class[]{Call.class}, new MyProxy(dog));
        call.call();
    }
}
package com.myspringboot.shejimoshi.daili.dongtai.jdk;

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

public class MyProxy implements InvocationHandler {
    Dog dog;

    public MyProxy(Dog dog) {
        this.dog = dog;
    }

    public void before() {
        System.out.println("method start..");
    }

    public void after() {
        System.out.println("method stop..");
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //Arrays.stream(proxy.getClass().getMethods()).map(Method::getName).forEach(System.out::println);

        before();
        Object obj = method.invoke(dog, args);
        after();
        return obj;
    }
}


这篇关于Java设计模式:代理模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程