静态代理

代码如下

package day19;
interface ClothFactory{
    public void show();
}
//被代理类;
class NikeFactory implements ClothFactory{

    @Override
    public void show() {
        // TODO Auto-generated method stub
        System.out.println("生产Nike衣服的工厂!");
    } 
    
}
//代理类;
class ProxyFactory implements ClothFactory{
    private ClothFactory factory ;
    
    public ProxyFactory(ClothFactory factory) {
        super();
        this.factory = factory;
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub
        factory.show(); //调用真正的被代理类的方法;
    }
    
}
public class StaticProxy {
public static void main(String[] args) {
    NikeFactory realFactory = new NikeFactory();
    ProxyFactory proxyFactory = new ProxyFactory(realFactory);
    proxyFactory.show();//实际是调用了NikeFactory的方法;
}
}

运行结果

709915-20190111182709681-943033496.png

转载于:https://www.cnblogs.com/SnailsRunning/p/10256604.html

你可能感兴趣的:(静态代理)