熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java設計模式-----Proxy模式(動態代理)

2013-11-23 18:50:55  來源: Java核心技術 

  Proxy模式(動態代理)

  這是JDK自帶的Proxy模式的實現方法

  例子

  view plaincopy to clipboardprint?
    public interface GirlInfo {
        public void hasBoyFriend();
    }

  public class Girl implements GirlInfo {

  public void hasBoyFriend() {
             Systemoutprintln(還沒有男朋友);
        }
    }

  public class GirlProxy implements InvocationHandler {

  private Object delegate;

  public Object bind(Object delegate) {
            thisdelegate = delegate;
            return ProxynewProxyInstance(delegategetClass()getClassLoader()
                    delegategetClass()getInterfaces() this);
        }

  public Object invoke(Object proxy Method method Object[] args)
                throws Throwable {

  methodinvoke(delegate args);
            return null;
        }
    }

  public class ProxyClient {

  public static void main(String[] args) {

  GirlProxy girlProxy = new GirlProxy();
            GirlInfo girl = (GirlInfo) girlProxybind(new Girl());
            girlhasBoyFriend();
        }
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25861.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.