Java--代理

静态代理举例

interface ClothFactory{
    void productCloth();
}


//代理类
class ProxyClothFactory implements ClothFactory{

    private ClothFactory factory;

    public ProxyClothFactory(ClothFactory factory) {
        this.factory = factory;
    }

    @Override
    public void productCloth() {
        System.out.println("我是衣服代理类");

        factory.productCloth();

        System.out.println("代理工厂后续工作");
    }
}

//被代理类
class NickClothFactory implements ClothFactory{
    @Override
    public void productCloth() {
        System.out.println("Nick工厂生产衣服");
    }
}

public class StaticProxy {
    public static void main(String[] args) {
        //创建被代理类
        NickClothFactory nickClothFactory=new NickClothFactory();
        //创建代理类
        ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nickClothFactory);

        proxyClothFactory.productCloth();
    }
}

动态代理举例

interface Human{

    int getAge();
    String getName();
}

//被代理类
class GDPeople implements Human{

    private int age;
    private String name;

    public GDPeople(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int getAge() {
        return age;
    }

    @Override
    public String getName() {
        return name;
    }
}

/**
    要想实现动态代理,需要解决的问题:
    1,如果根据加载到内存中的被代理类,动态创建一个代理类及其对象
    2,当通过代理类的对象调用方法时,如何动态的去调用被代理类中的同名方法

 */

class ProxyFactory{
    //调用此方法,返回一个代理类的对象。解决问题一
    public static Object getProxyInstance(Object obj){//obj:被代理类的对象

        MyInvocationHandler handler=new MyInvocationHandler();
        handler.bind(obj);
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handler);
    }
}

class MyInvocationHandler implements InvocationHandler {

    private Object object;//需要使用被代理类的对象进行赋值
    public void bind(Object object){
        this.object=object;
    }

    //当我们通过代理类的对象,调用方法a时,就会自动调用如下的方法:invoke()
    //当被代理类要执行的方法a功能就声明在invoke()中
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //method:即为代理类对象调用的方法,此方法也就作为了被代理类对象要调用的方法
        //obj:被代理类的对象
        Object invoke = method.invoke(object, args);
        //上述方法的返回值就作为当前类中invoke()的返回值
        return invoke;
    }
}

public class ProxyTest {

    public static void main(String[] args) {
        GDPeople gdPeople=new GDPeople(15,"Tom");
        Human proxyInstance = (Human) ProxyFactory.getProxyInstance(gdPeople);
        //当通过代理类对象调用方法时,会自动的调用被代理类中同名的方法
        int age = proxyInstance.getAge();
        String name = proxyInstance.getName();
        System.out.println("name:"+name+"-----age:"+age);
    }

}
原创文章 158 获赞 2 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/105454174
今日推荐