代理模式

代理简单来说,你把你自己能要做的事,让别人代你完成,比如你把作业交给课代表,课代表转交给老师

静态代理

interface Interface{
    void doSomething();
    void somethingElse(String arg);
}

class RealObject implements Interface{
    public void doSomething(){
        System.out.println("doSomething");
    }
    public void somethingElse(String arg){
        System.out.println("SomethingElse " + arg);
    }
}

class SimpleProxy implements Interface{
    private Interface proxied;
    public SimpleProxy(Interface proxied){
        this.proxied = proxied;
    }

    public void doSomething(){
        System.out.println("SimpleProxy doSomething");
        proxied.doSomething();
    }

    public void somethingElse(String arg){
        System.out.println("SimpleProxy somethingElse " + arg);
        proxied.somethingElse(arg);
    }
}

class SimpleProxyDemo{
    public static void consumer(Interface iface){
        iface.doSomething();
        iface.somethingElse("hello");
    }
}


public class Main{

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        SimpleProxyDemo.consumer(new RealObject());
        SimpleProxyDemo.consumer(new SimpleProxy(new RealObject()));
    }
}

运行结果:

doSomething
SomethingElse hello
SimpleProxy doSomething
doSomething
SimpleProxy somethingElse hello

SomethingElse hello


动态代理

interface Interface{
    void doSomething();
    void somethingElse(String arg);
}

class RealObject implements Interface{
    public void doSomething(){
        System.out.println("doSomething");
    }
    public void somethingElse(String arg){
        System.out.println("SomethingElse " + arg);
    }
}

class SimpleDynamicProxyDemo{
    public static void consumer(Interface iface){
        iface.doSomething();
        iface.somethingElse("hello");
    }
}



class DynamicProxyHandler implements InvocationHandler{
    private Object proxied;
    public DynamicProxyHandler(Object proxied){
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(proxied, args);
    }
}

public class Main{

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        RealObject real = new RealObject();
        SimpleDynamicProxyDemo.consumer(real);
        //代理对象
        Interface proxy = (Interface) Proxy.newProxyInstance(
                Interface.class.getClassLoader(),                  //类加载器
                new Class[]{ Interface.class},                     //继承的接口
                new DynamicProxyHandler(real)                      //需要代理的对象
        );
        SimpleDynamicProxyDemo.consumer(proxy);
    }
}

运行结果:

doSomething
SomethingElse hello
doSomething

SomethingElse hello

        与静态代理相比,动态代理需要手动去实现代理类,需要创建一个继承InvocationHandler接口的类,去帮我们实现;然后利用Proxy.newProxyInstance去生成代理对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值