1.Java静态代理举例:
代理类和被代理类在编译期间就已经确定下来了
1 interface ClothFactory{ 2 void produceCloth(); 3 } 4 5 class ProxyClothFactory implements ClothFactory{ 6 private ClothFactory factory; 7 public ProxyClothFactory(ClothFactory factory) { 8 this.factory = factory; 9 } 10 11 public void produceCloth() { 12 System.out.println("准备一些工作"); 13 factory.produceCloth(); 14 System.out.println("准备一些后续工作"); 15 } 16 } 17 18 class NikeClothFactory implements ClothFactory{ 19 @Override 20 public void produceCloth() { 21 System.out.println("Nike工厂生产一批衣服"); 22 } 23 } 24 25 public class StaticProxyTest { 26 public static void main(String[] args) { 27 //创建被代理类对象 28 NikeClothFactory nike = new NikeClothFactory(); 29 //创建代理类对象 30 ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike); 31 proxyClothFactory.produceCloth(); 32 } 33 }
原创文章,作者:wure,如若转载,请注明出处:https://blog.ytso.com/273626.html