静态代理与动态代理

静态的代码:

package java2_proxy;
interface ClothFactory{
    void produceCloth();
}

//  代理类
class ProxyClothFactory implements ClothFactory{
    private ClothFactory factory;
//    用被代理类的对象进行实例化
    public ProxyClothFactory(ClothFactory factory) {
        this.factory = factory;
    }

    @Override
    public void produceCloth() {
        System.out.println("代理工厂在工作: public void produceCloth()");
        factory.produceCloth();
        System.out.println("代理工厂后续工作:public void produceCloth()");
    }
}
//被代理类
class NikeClothFactory implements ClothFactory{

    @Override
    public void produceCloth() {
        System.out.println("NikeClothFactory  生产衣服");
    }
}
public class FieldTest {
    public static void main(String[] args) throws Exception {
//        被代理类的对象
        ClothFactory nikeClothFactory = new NikeClothFactory();
//        代理类的对象
        ClothFactory proxyClothFactory = new ProxyClothFactory(nikeClothFactory);
        proxyClothFactory.produceCloth();
    }
}

动态的代码:

package java2_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Human{
    String getBelief();
    void eat(String food);
}
class SuperMan implements Human{
    @Override
    public String getBelief() { return "i can fly";}
    @Override
    public void eat(String food) {
        System.out.println("i like" + food);
    }
}

//代理类
class ProxyFactory{
    public static Object getProxyInstance(Object object){
//        object 被代理类的对象
        MyInvocationHandler handler = new MyInvocationHandler();
        handler.bind(object);
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),handler);

    }
}
class HumanUtil{
    public void method1(){
        System.out.println("=============public void method1()==========");
    }
    public void method2(){
        System.out.println("=============public void method2()==========");
    }
}

class MyInvocationHandler implements InvocationHandler{
//  将被代理类要执行的方法
    private Object obj;
    public void bind(Object obj){
        this.obj = obj;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        HumanUtil humanUtil = new HumanUtil();
        humanUtil.method1();
        Object returnValue = method.invoke(obj, args);
        humanUtil.method2();
        return returnValue;
    }
}

public class ProxyTest {
    public static void main(String[] args) {
        SuperMan superMan = new SuperMan();
        Human proxyInstance = (Human) ProxyFactory.getProxyInstance(superMan);
        System.out.println(proxyInstance.getBelief());
        proxyInstance.eat("doufu");
        System.out.println("----------");
        java2_proxy.NikeClothFactory nikeClothFactory = new java2_proxy.NikeClothFactory();
        java2_proxy.ClothFactory proxyInstance1 = (java2_proxy.ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory);
        proxyInstance1.produceCloth();

    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
点赞 评论 收藏
分享
湫湫湫不会java:1.在校经历全删了2.。这些荣誉其实也没啥用只能说,要的是好的开发者不是好好学生3.项目五六点就行了,一个亮点一俩行,xxx技术解决,xxx问题带来xxx提升。第一页学历不行,然后啥有价值的信息也没有,到第二页看到项目了,第一个项目九点,第二个项目像凑数的俩点。总体给人又臭又长,一起加油吧兄弟
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务