1 //静态代理模式 2 //接口 3 interface ClothFactory{ 4 void productCloth(); 5 } 6 //被代理类 7 class NikeClothFactory implements ClothFactory{ 8 9 @Override 10 public void productCloth() { 11 System.out.println("Nike工厂生产一批衣服"); 12 } 13 } 14 //代理类 15 class ProxyFactory implements ClothFactory{ 16 ClothFactory cf; 17 //创建代理类的对象时,实际传入一个被代理类的对象 18 public ProxyFactory(ClothFactory cf){ 19 this.cf = cf; 20 } 21 22 @Override 23 public void productCloth() { 24 System.out.println("代理类开始执行,收代理费$1000"); 25 cf.productCloth(); 26 } 27 28 } 29 30 public class TestClothProduct { 31 public static void main(String[] args) { 32 NikeClothFactory nike = new NikeClothFactory();//创建被代理类的对象 33 ProxyFactory proxy = new ProxyFactory(nike);//创建代理类的对象 34 proxy.productCloth(); 35 } 36 }
时间: 2024-10-21 19:25:43