原创

代理模式之静态代理代码

package com.debuggg.java.exer2;

public class StaticProxyTest {

    public static void main(String[] args) {
        ClothFactory clothFactory = new ClothFactoryImpl();
        ProxyClothFactory proxyClothFactory = new ProxyClothFactory(clothFactory);
        proxyClothFactory.produceCloth();
    }

}

interface ClothFactory{
    void produceCloth();
}

/**
 * 作者 ZYL
 * 功能描述 : 代理类
 * 特点:编译期间,代理类和被代理类都被确定了
 * 日期 2020-03-01 11:26
 * 参数 null
 * 返回值
 */
class ProxyClothFactory implements ClothFactory{
    ClothFactory clothFactory;

    public ProxyClothFactory(ClothFactory clothFactory) {
        this.clothFactory = clothFactory;
    }

    @Override
    public void produceCloth() {
        System.out.println("代理工厂准备工作");
        clothFactory.produceCloth();
        System.out.println("代理工厂做一些后续的收尾工作");
    }
}

class ClothFactoryImpl implements ClothFactory{

    @Override
    public void produceCloth() {
        System.out.println("Nike工厂生产一批工作服");
    }
}
正文到此结束
本文目录