第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用相同參數(shù)的多個方法的設計模式

使用相同參數(shù)的多個方法的設計模式

BIG陽 2021-06-28 08:51:24
我有這個類,我有一個方法,將返回一個映射,在其中使用許多使用相同參數(shù)的方法。我的班級有數(shù)千行,而且還會增加。我可以創(chuàng)建多個類并在內部創(chuàng)建方法,但我問是否有針對此場景的特殊設計實際場景:public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {   Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);   lastMap = do1(firstMap, lastMap);   lastMap = do2(firstMap, lastMap);   lastMap = do3(firstMap, lastMap);   lastMap = do4(firstMap, lastMap);   //more 20 methods   return lastMap;}嘗試不設計:public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {    Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);    Do1 do1 = new Do1();    lastMap = do1.do1(firstMap, lastMap);    Do2 do2 = new Do2();    lastMap = do2.do2(firstMap, lastMap);    // more 20 methods    return lastMap;}
查看完整描述

3 回答

?
holdtom

TA貢獻1805條經(jīng)驗 獲得超10個贊

如果do1等只是實現(xiàn),而不是類的公共接口的一部分,那么創(chuàng)建一個私有類(甚至可能是嵌套類)可能是有意義的,它的構造函數(shù)接受它作為實例變量保留的兩個映射:


private static class Worker {

    Worker(Map firstMap, Map secondMap) {

        this.firstMap = firstMap;

        this.secondMap = secondMap;

    }

    void do1() {

        // ...update `this.lastMap` if appropriate...

    }

    void do2() {

        // ...update `this.lastMap` if appropriate...

    }

}

我在那里做了Worker static一個靜態(tài)嵌套類,而不是內部類,但static如果您需要訪問周圍類的內部結構,它可以是內部類(否)。


然后


public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {

     Worker worker = new Worker(firstMap, new HashMap<String, Object>(firstMap));

     worker.do1();

     worker.do2();

     worker.do3();

     worker.do4();

     //more 20 methods

     return worker.lastMap;

}


查看完整回答
反對 回復 2021-07-14
?
莫回無

TA貢獻1865條經(jīng)驗 獲得超7個贊

您可以使用interface并使每個“做”功能接口的實現(xiàn)。


interface Do {

    Map<String, Object> apply(Map<String, Object> firstMap, Map<String, Object> lastMap);

}

然后你可以初始化 static dos:


static Do[] allDos = {

    (firstMap, lastMap) -> {

        // do0

    },

    (firstMap, lastMap) -> {

        // do1

    },

    // ...do2, do3, ...

};

如果您需要調用do0 -> do2 -> do4例如:


public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {

    Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);

    int[] ids = { 0, 2, 4 };

    for (int id : ids)

        lastMap = allDos[id].apply(firstMap, lastMap);

    return lastMap;

}


查看完整回答
反對 回復 2021-07-14
?
慕的地6264312

TA貢獻1817條經(jīng)驗 獲得超6個贊

為什么不采取更多的功能方法?


定義接口


interface MapTransformation{ 

     void transformation(Map<String, Object>  first, Map<String, Object>  last);

}

然后在類創(chuàng)建期間,具有定義的類transformMapOnAnotherMap將轉換列表作為參數(shù)然后你可以做


class SomeClass {


    final private List<MapTransformation> transformations;


    //constructor omitted 


    public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {


        Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);

        transformations.forEach(t->t.transformation(firstMap,lastMap);

        return lastMap;


    }

}


查看完整回答
反對 回復 2021-07-14
  • 3 回答
  • 0 關注
  • 252 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號