Java的委託

來源:互聯網
上載者:User

標籤:

http://www.cnblogs.com/soojoe/archive/2012/04/12/2532304.html

 

委託模式是軟體設計模式中的一項基本技巧。在委託模式中,有兩個對象參與處理同一個請求,接受請求的對象將請求委託給另一個對象來處理。委託模式是一項基本技巧,許多其他的模式,如狀態模式、策略模式、訪問者模式 本質上是在更特殊的場合採用了委託模式。委託模式使得我們可以用彙總來替代繼承,它還使我們可以類比mixin。
  “委託”在C#中是一個語言級特性,而在Java語言中沒有直接的對應,但是我們可以通過動態代理來實現委託!代碼如下:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
   
public abstract class Delegator implements InvocationHandler {
    //--------------------------------------------

    protected Object obj_orgin = null; //原始對象
    protected Object obj_proxy = null; //代理對象
    //--------------------------------------------

    public Delegator() {
    }

    public Delegator(Object orgin) {
        this.createProxy(orgin);
    }
   

    protected Object createProxy(Object orgin) {
        obj_orgin = orgin;
        //下面語句中orgin.getClass().getClassLoader()為載入器,orgin.getClass().getInterfaces()為介面集
        obj_proxy = Proxy.newProxyInstance(orgin.getClass().getClassLoader(), orgin.getClass().getInterfaces(), this); //委託
        return obj_proxy;
    }
   

    protected Object invokeSuper(Method method, Object[] args) throws Throwable {
        return method.invoke(obj_orgin, args);
    }
    //--------------實現InvocationHandler介面,要求覆蓋------------
    //下面實現的方法是當委託的類調用toString()方法時,操作其他方法而不是該類預設的toString(),這個類的其他方法則不會。

    public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
        // 預設實現:委託給obj_orgin完成對應的操作
        if (method.getName().equals("toString")) { //對其做額外處理
            return this.invokeSuper(method, args) + "$Proxy";
        } else { //注意,調用原始對象的方法,而不是代理的(obj==obj_proxy)
            return this.invokeSuper(method, args);
        }
    }
}

下面的代碼,則是作為一個委託的例子,實現Map的功能。
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bs2.core.UtilLog;

public class Delegator4Map extends Delegator {
    private static Log _log = LogFactory.getLog(Delegator4Map.class);
    private Map orginClass = null; //原始對象
    private Map proxyClass = null; //代理對象

    public Map getOrgin() {
        return orginClass;
    }

    public Map getProxy() {
        return proxyClass;
    }

    public Delegator4Map(Map orgin) {
        super(orgin);
        orginClass = orgin;
        proxyClass = (Map) super.obj_proxy;
    }

    public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("size")) { //修改size處理邏輯
            Object res2 = new Integer(-1);
            System.out.println("調用委託的方法");
            return res2;
        } else {
            System.out.println("調用原始的方法");
            return super.invoke(obj, method, args);
        }
    }

    public static void main(String[] args) throws IOException {
        Delegator4Map rtm = new Delegator4Map(new Hashtable());
        Map m = rtm.getProxy();
        m.size();
    }
}

Java的委託

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.