代码如下:
package www.demo10; interface ClothFactory{ void productionCloth(); } //代理类 class Agency implements ClothFactory{ private ClothFactory clothFactory; public Agency(ClothFactory clothFactory){ this.clothFactory =clothFactory; } @Override public void productionCloth() { System.out.println("代理类"); clothFactory.productionCloth(); System.out.println("代理类。。。。。"); } } //被代理类 class ByProxy implements ClothFactory{ @Override public void productionCloth() { System.out.println("Nike最近在生产一批运动服"); } } public class StaticProxyTest { public static void main(String[] args) { ByProxy byProxy = new ByProxy(); Agency agency = new Agency(byProxy); agency.productionCloth(); } }
结果如下:
代理类 Nike最近在生产一批运动服 代理类。。。。。