如何最好地重构 Java 中的变量实例化重复?
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;
}
我在 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;
}