如何最好地重构Java中的变量实例化复制?

如何最好地重构Java中的变量实例化复制?,java,java-8,coding-style,refactoring,code-duplication,Java,Java 8,Coding Style,Refactoring,Code Duplication,我在Java中有两个方法收集不同的信息,但它们都设置并运行相同的过程,然后我们从中收集信息-数据的收集发生在循环中,我们实例化的变量也在循环中使用 Map<Integer, Integer> getResponsesWithCount(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets) throws InsufficientFundsException

我在Java中有两个方法收集不同的信息,但它们都设置并运行相同的过程,然后我们从中收集信息-数据的收集发生在循环中,我们实例化的变量也在循环中使用

 Map<Integer, Integer> getResponsesWithCount(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets) throws InsufficientFundsException {
    final int stake = getStake(baseCostMultiplier, stakeCostUuid);
    long balance = 10L * stake;
    final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));

    final Map<Integer, Integer> responseCounts = new HashMap<>();
    for (int count = 0; count < totalBets; count++) {
        final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);
        // If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
        balance = checkBalance(stake, balance, result._1(), count);
        final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);
        responseCounts.putIfAbsent(byteLength, 0);
        responseCounts.put(byteLength, responseCounts.get(byteLength) + 1);
    }
    return responseCounts;
}

Map<Integer, Integer> getResponsesWithPayouts(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets) throws InsufficientFundsException{
    final int stake = getStake(baseCostMultiplier, stakeCostUuid);
    long balance = 10L * stake;
    final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));
    final Map<Integer, Integer> responseCounts = new HashMap<>();
    for (int count = 0; count < totalBets; count++) {
        final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);

        // If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
        balance = checkBalance(stake, balance, result._1(), count);

        final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);
        final PlayData playData = result._2.getLastResponse().map(s -> new Gson().fromJson(s, GdkPlayData.class)).orElse(new GdkPlayData());
        final java.util.List<SlotsActionData> actionData = playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
        final int sumOfPayouts = actionData.stream()
                                           .map(SlotsActionData::getPayouts)
                                           .mapToInt(java.util.List::size)
                                           .sum();
        responseCounts.putIfAbsent(byteLength, sumOfPayouts);
    }
    return responseCounts;
}
Map getResponsesWithCount(int baseCostMultiplier、int revels、int visibleSymbols、String stakeCostUuid、int totalBets)引发的FundsException不足{
最终int stack=getstack(baseCostMultiplier,stakeCostUuid);
长天平=10L*桩;
final testgpaspplatform testgpaspplatform=testgpaspplatform.create(ryotaAdapter,testgpaspplatform.DEFAULT\u MIN\u BET,Math.max(stack,testgpaspplatform.DEFAULT\u max\u BET),testgpaspplatform.DEFAULT\u max\u WIN,ImmutableList.of(baseCostMultiplier));
final-Map-responseCounts=new-HashMap();
对于(int count=0;counts.getBytes().length).orElse(0);
响应计数putIfAbsent(通过电话长度,0);
responseCounts.put(byteLength,responseCounts.get)(byteLength)+1;
}
返回响应计数;
}
Map GetResponseWithPayouts(int BaseCost乘数、int卷轴、int可见符号、字符串stakeCostUuid、int totalBets)抛出不足的FundsException{
最终int stack=getstack(baseCostMultiplier,stakeCostUuid);
长天平=10L*桩;
final testgpaspplatform testgpaspplatform=testgpaspplatform.create(ryotaAdapter,testgpaspplatform.DEFAULT\u MIN\u BET,Math.max(stack,testgpaspplatform.DEFAULT\u max\u BET),testgpaspplatform.DEFAULT\u max\u WIN,ImmutableList.of(baseCostMultiplier));
final-Map-responseCounts=new-HashMap();
对于(int count=0;counts.getBytes().length).orElse(0);
最终播放数据PlayData=result.\u 2.getLastResponse().map(s->new Gson().fromJson(s,GdkPlayData.class)).orElse(new GdkPlayData());
final java.util.List actionData=playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
最终int-sumOfPayouts=actionData.stream()
.map(SlotsActionData::getPayouts)
.mapToInt(java.util.List::size)
.sum();
响应费用(通过电话、付款总额);
}
返回响应计数;
}
每种方法的前6行代码都完全重复,但我不确定应该或可以如何清理这些代码


我认为这个问题的一个扩展是,我有两个方法调用链,它们对收集的数据以外的所有数据执行相同的操作,我没有像我认为的那样使用布尔运算符来拆分此功能,而是实现了一个新的方法链来完成它。我是否应该以不同的方式执行此操作?

您可以创建一个通用方法和传递类型,如
with count
,如下所示

Map<Integer, Integer> getResponses(int baseCostMultiplier, int reels, int visibleSymbols, String stakeCostUuid, int totalBets, boolean withCount) throws InsufficientFundsException {
    final int stake = getStake(baseCostMultiplier, stakeCostUuid);
    long balance = 10L * stake;
    final TestGpasPlatform testGpasPlatform = TestGpasPlatform.create(ryotaAdapter, TestGpasPlatform.DEFAULT_MIN_BET, Math.max(stake, TestGpasPlatform.DEFAULT_MAX_BET), TestGpasPlatform.DEFAULT_MAX_WIN, ImmutableList.of(baseCostMultiplier));

    final Map<Integer, Integer> responseCounts = new HashMap<>();
    for (int count = 0; count < totalBets; count++) {
        final Tuple2<List<Output>, TestGpasPlatform> result = playWithRealRng(baseCostMultiplier, count, reels, visibleSymbols, stakeCostUuid, testGpasPlatform);
        // If we run out of balance, re-start, we want to do meaningful spins that trigger features, etc
        balance = checkBalance(stake, balance, result._1(), count);
        final int byteLength = result._2.getLastResponse().map(s -> s.getBytes().length).orElse(0);

        if(withCount) {
            responseCounts.putIfAbsent(byteLength, 0);
            responseCounts.put(byteLength, responseCounts.get(byteLength) + 1);
        }else{
            final PlayData playData = result._2.getLastResponse().map(s -> new Gson().fromJson(s, GdkPlayData.class)).orElse(new GdkPlayData());
            final java.util.List<SlotsActionData> actionData = playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
            final int sumOfPayouts = actionData.stream()
                    .map(SlotsActionData::getPayouts)
                    .mapToInt(java.util.List::size)
                    .sum();
            responseCounts.putIfAbsent(byteLength, sumOfPayouts);
        }
    }
    return responseCounts;
}
Map getResponses(int baseCostMultiplier、int reels、int VisibleSynumbles、String stakeCostUuid、int totalBets、boolean withCount)引发的FundsException不足{
最终int stack=getstack(baseCostMultiplier,stakeCostUuid);
长天平=10L*桩;
final testgpaspplatform testgpaspplatform=testgpaspplatform.create(ryotaAdapter,testgpaspplatform.DEFAULT\u MIN\u BET,Math.max(stack,testgpaspplatform.DEFAULT\u max\u BET),testgpaspplatform.DEFAULT\u max\u WIN,ImmutableList.of(baseCostMultiplier));
final-Map-responseCounts=new-HashMap();
对于(int count=0;counts.getBytes().length).orElse(0);
如果(带计数){
响应计数putIfAbsent(通过电话长度,0);
responseCounts.put(byteLength,responseCounts.get)(byteLength)+1;
}否则{
最终播放数据PlayData=result.\u 2.getLastResponse().map(s->new Gson().fromJson(s,GdkPlayData.class)).orElse(new GdkPlayData());
final java.util.List actionData=playData.findLastPlay().getLastPlayInModeData().getSlotsData().getActions();
最终int-sumOfPayouts=actionData.stream()
.map(SlotsActionData::getPayouts)
.mapToInt(java.util.List::size)
.sum();
响应费用(通过电话、付款总额);
}
}
返回响应计数;
}

选择重复的代码部分,并根据您的IDE使用“提取到方法”快捷方式。它不适用于特定的选定块。正如我在文章中所说,它在循环之前开始,在循环中结束,这一事实使得重构变得困难。IDEA根本没有提供解决方案。也许我可以创建一个对象来保存每个方法开头4行中的变量,并在每个方法中使用该对象,而不是两次实例化4个变量?您可以创建一个方法来完成所有这些工作,并接受它设置的任何消费者