Proxy

一、Static Proxy

interface HelloService {
    public void hello(String msg);
}

class HelloServiceImpl implements HelloService {
    public void hello(String msg) {
        System.out.println("hello, " + msg);
    }
}

class HelloServiceProxy implements HelloService {
    private HelloService helloService;

    public HelloServiceProxy(HelloService helloService) {
        this.helloService = helloService;
    }

    public void hello(String msg) {
        helloService.hello(msg);
    }
}

public class StaticProxyExample {
    public static void main(String[] args) {
        HelloService helloService = new HelloServiceImpl();
        HelloService helloServiceProxy = new HelloServiceProxy(helloService);
        helloServiceProxy.hello("world!");
    }
}

二、Dynamic Proxy

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

class MyInvocationHandler implements InvocationHandler {
    private Object target;

    private MyInvocationHandler(Object target) {
        this.target = target;
    }

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

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        return result;
    }
}

public class DynamicProxyExample {
    public static void main(String[] args) {
        HelloService helloService = new HelloServiceImpl();
        HelloService helloServiceProxy = (HelloService) MyInvocationHandler.createProxy(helloService);
        helloServiceProxy.hello("world!");
    }
}

三、TracingProxy

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

interface Dog {
    void bark();
}

class DogImpl implements Dog {
    public void bark() {
    }
}

class TracingIH implements InvocationHandler {
    private Object target;

    private TracingIH(Object obj) {
        target = obj;
    }

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

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        try {
            System.out.println(method.getName() + "() called");
            result = method.invoke(target, args);
        } catch (InvocationTargetException e) {
            System.out.println(method.getName() + " throws " + e.getCause());
            throw e.getCause();
        }
        System.out.println(method.getName() + "() returns");
        return result;
    }
}

class DogFactory {
    private Class<?> dogClass;
    private boolean traceIsOn = false;

    public DogFactory(String className, boolean trace) {
        try {
            dogClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e); // or whatever is appropriate
        }
        traceIsOn = trace;
    }

    public Dog newInstance() {
        try {
            Dog d = (Dog) dogClass.newInstance();
            if (traceIsOn) {
                d = (Dog) TracingIH.createProxy(d);
            }
            return d;
        } catch (InstantiationException e) {
            throw new RuntimeException(e); // or whatever is appropriate
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e); // or whatever is appropriate
        }
    }
}

public class TracingProxyExample {
    public static void main(String[] args) {
        DogFactory factory = new DogFactory("DogImpl", true);
        Dog dog = (Dog) factory.newInstance();
        dog.bark();
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值