Java代理

简单代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package typeinfo;
 
interface Interface {
    void doSomething();
 
    void doSomethingElse(String arg);
}
 
class RealObject implements Interface {
 
    @Override
    public void doSomething() {
        System.out.println("doSomething");
    }
 
    @Override
    public void doSomethingElse(String arg) {
        System.out.println("SomethingElse " + arg);
    }
}
 
class SimpleProxy implements Interface {
    private Interface proxied;
 
    public SimpleProxy(Interface proxied) {
        this.proxied = proxied;
    }
 
    @Override
    public void doSomething() {
        System.out.println("SimpleProxy doSomething");
        proxied.doSomething();
    }
 
    @Override
    public void doSomethingElse(String arg) {
        System.out.println("SimpleProxy doSomethingElse" + arg);
        proxied.doSomethingElse(arg);
    }
}
 
public class SimpleProxyDemo {
    public static void consumer(Interface iface){
        iface.doSomething();
        iface.doSomethingElse("bonobo");
    }
 
    public static void main(String[] args) {
        consumer(new RealObject());
        consumer(new SimpleProxy(new RealObject()));
    }
}

动态代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package typeinfo;
 
import java.lang.reflect.*;
 
class DynamicProxyHandle implements InvocationHandler {
    private Object proxied;
 
    public DynamicProxyHandle(Object proxied) {
        this.proxied = proxied;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("****** proxy: " + proxy.getClass() + ", method: " + method + ", args: " + args);
        if (args != null) {
            for (Object arg : args)
                System.out.println("  " + arg);
        }
        return method.invoke(proxied, args);
    }
}
 
public class SimpleDynamicProxy {
    public static void consumer(Interface iface) {
        iface.doSomething();
        iface.doSomethingElse("bonobo");
    }
 
    public static void main(String[] args) {
        RealObject real = new RealObject();
        consumer(real);
        //Insert a proxy and call again:
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
                new Class[]{Interface.class},
                new DynamicProxyHandle(real));
        consumer(proxy);
    }
}

可以查看方法名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package typeinfo;
 
import java.lang.reflect.*;
 
class MethodSelector implements InvocationHandler {
    private Object proxied;
 
    public MethodSelector(Object proxied) {
        this.proxied = proxied;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("interesting"))
            System.out.println("Proxy detected the interesting method");
        return method.invoke(proxied, args);
    }
}
 
interface SomeMethods {
    void boring1();
 
    void boring2();
 
    void interesting(String arg);
 
    void boring3();
}
 
class Implementaction implements SomeMethods {
 
    @Override
    public void boring1() {
        System.out.println("boring1");
    }
 
    @Override
    public void boring2() {
        System.out.println("boring2");
    }
 
    @Override
    public void interesting(String arg) {
        System.out.println("interesting " + arg);
    }
 
    @Override
    public void boring3() {
        System.out.println("boring3");
    }
}
 
class SelectingMethods {
    public static void main(String[] args) {
        SomeMethods proxy = (SomeMethods)Proxy.newProxyInstance(
                SomeMethods.class.getClassLoader(),
                new Class[]{SomeMethods.class},
                new MethodSelector(new Implementaction())
        );
        proxy.boring1();
        proxy.boring2();
        proxy.interesting("bonobo");
        proxy.boring3();
    }
}