使用相同参数的多个方法的设计模式

我有这个类,我有一个方法,将返回一个映射,在其中使用许多使用相同参数的方法。


我的班级有数千行,而且还会增加。


我可以创建多个类并在内部创建方法,但我问是否有针对此场景的特殊设计


实际场景:


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;


}


BIG阳
浏览 235回答 3
3回答

holdtom

如果do1等只是实现,而不是类的公共接口的一部分,那么创建一个私有类(甚至可能是嵌套类)可能是有意义的,它的构造函数接受它作为实例变量保留的两个映射:private static class Worker {&nbsp; &nbsp; Worker(Map firstMap, Map secondMap) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstMap = firstMap;&nbsp; &nbsp; &nbsp; &nbsp; this.secondMap = secondMap;&nbsp; &nbsp; }&nbsp; &nbsp; void do1() {&nbsp; &nbsp; &nbsp; &nbsp; // ...update `this.lastMap` if appropriate...&nbsp; &nbsp; }&nbsp; &nbsp; void do2() {&nbsp; &nbsp; &nbsp; &nbsp; // ...update `this.lastMap` if appropriate...&nbsp; &nbsp; }}我在那里做了Worker static一个静态嵌套类,而不是内部类,但static如果您需要访问周围类的内部结构,它可以是内部类(否)。然后public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {&nbsp; &nbsp; &nbsp;Worker worker = new Worker(firstMap, new HashMap<String, Object>(firstMap));&nbsp; &nbsp; &nbsp;worker.do1();&nbsp; &nbsp; &nbsp;worker.do2();&nbsp; &nbsp; &nbsp;worker.do3();&nbsp; &nbsp; &nbsp;worker.do4();&nbsp; &nbsp; &nbsp;//more 20 methods&nbsp; &nbsp; &nbsp;return worker.lastMap;}

莫回无

您可以使用interface并使每个“做”功能接口的实现。interface Do {&nbsp; &nbsp; Map<String, Object> apply(Map<String, Object> firstMap, Map<String, Object> lastMap);}然后你可以初始化 static dos:static Do[] allDos = {&nbsp; &nbsp; (firstMap, lastMap) -> {&nbsp; &nbsp; &nbsp; &nbsp; // do0&nbsp; &nbsp; },&nbsp; &nbsp; (firstMap, lastMap) -> {&nbsp; &nbsp; &nbsp; &nbsp; // do1&nbsp; &nbsp; },&nbsp; &nbsp; // ...do2, do3, ...};如果您需要调用do0 -> do2 -> do4例如:public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {&nbsp; &nbsp; Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);&nbsp; &nbsp; int[] ids = { 0, 2, 4 };&nbsp; &nbsp; for (int id : ids)&nbsp; &nbsp; &nbsp; &nbsp; lastMap = allDos[id].apply(firstMap, lastMap);&nbsp; &nbsp; return lastMap;}

慕的地6264312

为什么不采取更多的功能方法?定义接口interface MapTransformation{&nbsp;&nbsp; &nbsp; &nbsp;void transformation(Map<String, Object>&nbsp; first, Map<String, Object>&nbsp; last);}然后在类创建期间,具有定义的类transformMapOnAnotherMap将转换列表作为参数然后你可以做class SomeClass {&nbsp; &nbsp; final private List<MapTransformation> transformations;&nbsp; &nbsp; //constructor omitted&nbsp;&nbsp; &nbsp; public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);&nbsp; &nbsp; &nbsp; &nbsp; transformations.forEach(t->t.transformation(firstMap,lastMap);&nbsp; &nbsp; &nbsp; &nbsp; return lastMap;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java