設計模式之責任鏈模式

責任鏈模式是一種對象的行爲模式。

在責任鏈模式裏,使多個對象都有機會出來請求,從而避免了請求的發送者和接收者之間的耦合關係。將這些對象連成一條鏈,並沿着這條鏈傳遞該請求。

 



 

 

        責任鏈模式的顯著優點就是講請求和處理分開,請求者可以不用知道是誰處理的,處理者可以不用知道請求的全貌,責任鏈的一個顯著缺點就是性能問題,因爲每個請求都是從鏈頭遍歷的到鏈尾,特別是在鏈比較長的時候,性能是一個非常大的問題,還有一個問題就是調試很不方便,由於採用了類似遞歸的方式,調試的時候邏輯可能會比較複雜。

 

在命令模式中說了一個自己項目的的查詢模塊,只是查詢的類型不同,有根據訂單號查詢、票號查詢、退款訂單查詢、業務系統訂單號查詢、合作方訂單號查詢等等

 



 

頁面如上圖,下面就用責任鏈模式來實現上述功能,類圖如下:

 






 
 
 
 

public class QueryVO {

    // 查詢類型
    private String type;

    // 查詢內容
    private String value;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

 

/**
 * 訂單號查詢
 * 
 */
public class OrderNoQuery extends QueryHandler {


    public OrderNoQuery() {

        super.queryType ="OrderNO";
    }

    /**
     * @param vo
     * @return
     */
    @Override
    protected String exec(QueryVO vo) {
        System.out.println("-------根據訂單號查詢---------");
        System.out.println(vo.getValue());
        return vo.getValue();
    }

}

 

/**
 * 票號查詢
 * 
 */
public class TktNoQuery extends QueryHandler {

    

    public TktNoQuery() {

        super.queryType = "TktNo";
    }
    
    /**
     * @param vo
     * @return
     */
    @Override
    protected String exec(QueryVO vo) {
       
        System.out.println("-------根據票號查詢---------");
        System.out.println(vo.getValue());
        return vo.getValue();
    }

}

 

/**
 * 退款單號查詢
 * 
 */
public class RefundNoQuery extends QueryHandler {

    

    public RefundNoQuery() {

        super.queryType = "RefundNO";
    }
    
    /**
     * @param vo
     * @return
     */
    @Override
    protected String exec(QueryVO vo) {

        System.out.println("-------根據退款單號查詢---------");
        System.out.println(vo.getValue());
        return vo.getValue();
    }

}

 

public abstract class QueryHandler {

    // 下一個執行者
    private QueryHandler next;

    // 查詢類型
    protected String queryType;

    // 查詢
    protected final String query(QueryVO vo) {

        // 判斷當前鏈能不能出來該查詢
        if (vo.getType().equalsIgnoreCase(this.queryType)) {
            return exec(vo);
        } else {
            // 不能出來的話交由下一級處理
            return this.next.query(vo);
        }
    }

    // 具體查詢邏輯交由子類去實現
    protected abstract String exec(QueryVO vo);

    public QueryHandler getNext() {
        return next;
    }

    public void setNext(QueryHandler next) {
        this.next = next;
    }

}

 

public class Client {

    private QueryHandler orderNoHandler;

    // 初始化責任鏈
    public Client() {

        orderNoHandler = new OrderNoQuery();

        QueryHandler tktNoHandler = new TktNoQuery();

        QueryHandler refundNOHandler = new RefundNoQuery();

        orderNoHandler.setNext(tktNoHandler);

        tktNoHandler.setNext(refundNOHandler);
    }

    public void query(QueryVO vo) {

        this.orderNoHandler.query(vo);

    }

    public static void main(String[] args) {

        Client client = new Client();
        QueryVO vo = new QueryVO();

        vo.setType("RefundNO");
        vo.setValue("1000344500343");

        client.query(vo);

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章