JAVA#静态代理'札记

  public static void main(String[] args) {
        NikeClothFactory ncf=new NikeClothFactory();//被代理类的对象
        ProxyFactory pf=new ProxyFactory(ncf);//代理类实例化,将被代理类的对象传入构造器中
        pf.productCloth();
    }
}
interface Clothproduct{//接口
    void productCloth();
}
class NikeClothFactory implements Clothproduct{//被代理类
    public void productCloth(){
        System.out.println("nike make cloth");//重写方法
    }
}
class ProxyFactory implements Clothproduct{//代理类
    Clothproduct cp;//被代理类的对象
    public ProxyFactory(Clothproduct cp){
        this.cp=cp;
    }

    @Override
    public void productCloth() {
        System.out.println("proxy factory");
        cp.productCloth();//调用被代理类的制衣方法
    }
}
proxy factory
nike make cloth

猜你喜欢

转载自blog.csdn.net/Iverson941112/article/details/86378633
今日推荐