我在 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;
}
每个方法的前 6 行代码完全重复,但我不确定我应该或如何清理它。
我认为这个问题的扩展是我有两个方法调用链,它们对除收集的数据之外的所有数据执行相同的操作,而不是使用 boolean 运算符来分割此功能,因为我认为这很糟糕设计时,我实现了一系列新的方法来完成它。我应该采取不同的做法吗?
最佳答案
您可以创建一个通用方法并传递类型,例如 withCount
,如下所示,
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;
}
关于java - 如何最好地重构 Java 中的变量实例化重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60522983/