首页 > 技术文章 > 代理类和被代理类的解释及案例

SKTT1 2017-12-02 13:33 原文

package com.eduask.dl;

首先定义一个接口
interface ClothFactory {
void productCloth();
}

创建被代理类
class NikeClothFactory implements ClothFactory{

@Override
     public void productCloth() {
             System.out.println("NIKE工厂生产了一批衣服");
         }
}
创建代理类
            class ProxyFactory implements ClothFactory{
                              ClothFactory cf;
                       将被代理类放入代理类
                              public ProxyFactory(ClothFactory cf){
                                       this.cf = cf;
               }
@Override
         public void productCloth() {
                  System.out.println("代理类开始执行,代理费$1000");
                        cf.productCloth();
           }
}

public class TestClothProduct {
                 public static void main(String[] args) {
                         NikeClothFactory nike = new NikeClothFactory();
                          ProxyFactory proxy = new ProxyFactory(nike);
                                  proxy.productCloth();
}
}

推荐阅读