1. package com.atguigu.java1;
    2. /**
    3. * 静态代理举例:
    4. *
    5. * 特点:代理类和被代理类在编译期间,就确定下来了。
    6. *
    7. * @author Dxkstart
    8. * @create 2021-06-08 11:13
    9. */
    10. interface ClothFactory{
    11. void produceCloth();
    12. }
    13. //代理类
    14. class ProxyClothFactory implements ClothFactory{
    15. private ClothFactory factory;//用被代理类对象进行实例化
    16. //构造器
    17. public ProxyClothFactory(ClothFactory factory){
    18. this.factory = factory;
    19. }
    20. @Override
    21. public void produceCloth() {
    22. System.out.println("代理工厂做一些准备工作");
    23. factory.produceCloth();
    24. System.out.println("代理工厂做一些后续的首尾工作");
    25. }
    26. }
    27. //被代理类
    28. class NikeClothFactory implements ClothFactory{
    29. @Override
    30. public void produceCloth() {
    31. System.out.println("Nike工厂生产一批运动服");
    32. }
    33. }
    34. public class StaticProxyTest {
    35. public static void main(String[] args) {
    36. //1.创建被代理类的对象
    37. ClothFactory nike = new NikeClothFactory();
    38. //2.创建代理类的对象
    39. ClothFactory proxyClothFactory = new ProxyClothFactory(nike);
    40. proxyClothFactory.produceCloth();
    41. }
    42. }