public CountRecoder getCountRecoder(List<CountEntry> countEntries) {
CountRecoder countRecoder = new CountRecoder();
for (CountEntry countEntry : countEntries) {
if (1 == countEntry.getCode()) {
countRecoder.setCountOfFirstStage(countEntry.getCount());
} else if (2 == countEntry.getCode()) {
countRecoder.setCountOfSecondStage(countEntry.getCount());
} else if (3 == countEntry.getCode()) {
countRecoder.setCountOfThirdtage(countEntry.getCount());
} else if (4 == countEntry.getCode()) {
countRecoder.setCountOfForthtage(countEntry.getCount());
} else if (5 == countEntry.getCode()) {
countRecoder.setCountOfFirthStage(countEntry.getCount());
} else if (6 == countEntry.getCode()) {
countRecoder.setCountOfSixthStage(countEntry.getCount());
}
}
return countRecoder;
}
private static Map<Integer, String> methodsMap = new HashMap<>();
static {
methodsMap.put(1, "setCountOfFirstStage");
methodsMap.put(2, "setCountOfSecondStage");
methodsMap.put(3, "setCountOfThirdtage");
methodsMap.put(4, "setCountOfForthtage");
methodsMap.put(5, "setCountOfFirthStage");
methodsMap.put(6, "setCountOfSixthStage");
}
public CountRecoder getCountRecoderByReflect(List<CountEntry> countEntries) {
CountRecoder countRecoder = new CountRecoder();
countEntries.stream().forEach(countEntry -> fillCount(countRecoder, countEntry));
return countRecoder;
}
private void fillCount(CountRecoder shippingOrderCountDto, CountEntry countEntry) {
String name = methodsMap.get(countEntry.getCode());
try {
Method declaredMethod = CountRecoder.class.getMethod(name, Integer.class);
declaredMethod.invoke(shippingOrderCountDto, countEntry.getCount());
} catch (Exception e) {
System.out.println(e);
}
}
public interface FillCountService {
void fillCount(CountRecoder countRecoder, int count);
}
class FirstStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfFirstStage(count);
}
}
class SecondStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfSecondStage(count);
}
}
class ThirdStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfThirdtage(count);
}
}
class ForthStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfForthtage(count);
}
}
class FirthStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfFirthStage(count);
}
}
class SixthStageService implements FillCountService {
@Override
public void fillCount(CountRecoder countRecoder, int count) {
countRecoder.setCountOfSixthStage(count);
}
}
public class FillCountServieFactory {
private static Map<Integer, FillCountService> fillCountServiceMap = new HashMap<>();
static {
fillCountServiceMap.put(1, new FirstStageService());
fillCountServiceMap.put(2, new SecondStageService());
fillCountServiceMap.put(3, new ThirdStageService());
fillCountServiceMap.put(4, new ForthStageService());
fillCountServiceMap.put(5, new FirthStageService());
fillCountServiceMap.put(6, new SixthStageService());
}
public static FillCountService getFillCountStrategy(int statusCode) {
return fillCountServiceMap.get(statusCode);
}
}
public CountRecoder getCountRecoder(List<CountEntry> countEntries) {
CountRecoder countRecoder = new CountRecoder();
countEntries.stream().forEach(countEntry ->
FillCountServieFactory.getFillCountStrategy(countEntry.getCode())
.fillCount(countRecoder, countEntry.getCount()));
return countRecoder;
}
@FunctionalInterface
public interface FillCountService {
void fillCount(CountRecoder countRecoder, int count);
}
(countRecoder, count) -> countRecoder.setCountOfFirstStage(count)
public class FillCountServieFactory {
private static Map<Integer, FillCountService> fillCountServiceMap = new HashMap<>();
static {
fillCountServiceMap.put(1, (countRecoder, count) -> countRecoder.setCountOfFirstStage(count));
fillCountServiceMap.put(2, (countRecoder, count) -> countRecoder.setCountOfSecondStage(count));
fillCountServiceMap.put(3, (countRecoder, count) -> countRecoder.setCountOfThirdtage(count));
fillCountServiceMap.put(4, (countRecoder, count) -> countRecoder.setCountOfForthtage(count));
fillCountServiceMap.put(5, (countRecoder, count) -> countRecoder.setCountOfFirthStage(count));
fillCountServiceMap.put(6, (countRecoder, count) -> countRecoder.setCountOfSixthStage(count));
}
public static FillCountService getFillCountStrategy(int statusCode) {
return fillCountServiceMap.get(statusCode);
}
}
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。