如何避免 Java 中的方法重载以避免重复?
How to avoid Method Overloading in Java to avoid duplicates?
我有 2 种方法执行 80% 相同的工作,但结果处理不同。我在做 :
private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = objectMapper.readValue(reader, responseClass);
^
// DIFFERENCE --------------------------------|
}
return response;
}
private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = objectMapper.readValue(reader, valueTypeRef);
^
// DIFFERENCE --------------------------------|
}
return response;
}
这看起来像很多重复项。我该怎么做才能减少重复项。
您可以使用调用 objectMapper
:
的 BiFunction
参数
private <T> T getResponse(final RestURI query,
final BiFunction<CustomReader, ObjectMapper, T> mapper) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = mapper.apply(reader, objectMapper);
}
return response;
}
并且呼叫者可以选择如何呼叫objectMapper
:
getResponse(query,
(reader, objectMapper) -> objectMapper.readValue(reader, <valueTypeRef>);
或:
getResponse(query,
(reader, objectMapper) -> objectMapper.readValue(reader, <class>);
我有 2 种方法执行 80% 相同的工作,但结果处理不同。我在做 :
private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = objectMapper.readValue(reader, responseClass);
^
// DIFFERENCE --------------------------------|
}
return response;
}
private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = objectMapper.readValue(reader, valueTypeRef);
^
// DIFFERENCE --------------------------------|
}
return response;
}
这看起来像很多重复项。我该怎么做才能减少重复项。
您可以使用调用 objectMapper
:
BiFunction
参数
private <T> T getResponse(final RestURI query,
final BiFunction<CustomReader, ObjectMapper, T> mapper) throws IOException {
T response = null;
final RestResponse<Record> tempResponse = getResponseFromDataPath(query);
if (isResponseOK(tempResponse, query)) {
final CustomReader reader = createCustomReaderFromResponse(tempResponse);
response = mapper.apply(reader, objectMapper);
}
return response;
}
并且呼叫者可以选择如何呼叫objectMapper
:
getResponse(query,
(reader, objectMapper) -> objectMapper.readValue(reader, <valueTypeRef>);
或:
getResponse(query,
(reader, objectMapper) -> objectMapper.readValue(reader, <class>);