The func1 method of TestClass calls fillIdsAndNames, and different fillIdsAndNames functions internally modify Ids and names, which is a bad smell. Whether the bad smell can be eliminated by refactoring and design-pattern without leading other bad smells.
interface Computable {
void fillIdsAndNames(Integer num, Set<String> ids, Set<String> names);
}
@Component
class Class1 implements Computable {
@Override
public void fillIdsAndNames(Integer num, Set<String> ids, Set<String> names) {
Set<String> candidateIds = computeFunc1(num);
candidateIds.stream().filter(id -> !ids.contains(id)).forEach(id -> {
ids.add(id);
names.addAll(someFunc1(id));
});
}
}
@Component
class Class2 implements Computable {
@Override
public void fillIdsAndNames(Integer num, Set<String> ids, Set<String> names) {
if (ids.contains(num)) {
return;
}
Set<String> candidateIds = computeFunc2(num);
for (String id : candidateIds) {
if (judgeFunc(id)) {
ids.add(id);
} else {
names.add(someFunc2(id));
}
}
}
}
@Component
public class TestClass {
@Autowired
private Map<Integer, Computable> map;
void func1(List<Integer> nums) {
Set<String> ids = new HashSet<>();
Set<String> names = new HashSet<>();
for (Integer num : nums) {
Computable computer = map.get(num);
computer.fillIdsAndNames(num, ids, names);
}
.....
}
}
Aucun commentaire:
Enregistrer un commentaire