[英]How best to refactor variable instantiation duplication in Java?
我在 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 行代碼都完全重復,但我不確定我應該或可以如何清理它。
我認為這個問題的一個擴展是我有兩條方法調用鏈,它們對收集的數據以外的所有數據都做同樣的事情,而不是像我認為的那樣用一個布爾運算符來拆分這個功能,因為這是糟糕的設計,我實施了一個新的方法鏈來完成它。 我應該以不同的方式這樣做嗎?
您可以創建一種通用方法和傳遞類型,如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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.