Java 两个或多个具有相同代码的方法中的代码重用
Java code reuse in two or more methods with same code
我在 Java 中有以下代码来查询数据库:
public interface MapReduceDAO {
String host = "mysql";
int port = 3306;
String user = "root";
String password = "root";
String dbName = "customers";
default String customersMysqlUrl(String name) {
return getDocker().containers().container(name).port(port).inFormat("$HOST:$EXTERNAL_PORT");
}
default void checkTableHasData(Duration atMost, String tableName) throws Exception {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
is(Matchers.greaterThan(0)));
}
}
default void checkExistsQuery(Duration atMost, String tableName, int countValueExpected) throws Exception {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
is(Matchers.equalTo(countValueExpected)));
}
}
DockerComposeRule getDocker();
}
如何避免使用重复代码。在方法 checkTableHasData 和 checkExistsQuery 中,我大部分都是重复代码。
编辑:忘记说了,他们最后可能有不同的断言,例如:
is(Matchers.greaterThan(0)));
is(Matchers.equalTo(countValueExpected)));
如果我没看错的话,这些方法仅在您提供给 count()
的参数上有所不同。只是引入一个方法,将this作为参数,并用不同的值调用它。
default void checkTableHasData(Duration atMost, String tableName) throws Exception {
check(atMost, "SELECT COUNT(*) FROM " + tableName);
}
default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
String columnValue) throws Exception {
check(atMost, "SELECT COUNT(*) FROM " + tableName + " where " + columnName +
" = " + columnValue);
}
private void check(Duration atMost, String countStatement) throws Exception {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count(countStatement),
is(Matchers.greaterThan(0)));
}
}
您可以在单独的方法中简单地提取常见行为:
default void checkTableHasData(Duration atMost, String tableName) throws Exception {
checkExistsQuery("SELECT COUNT(*) FROM " + tableName),
}
default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
String columnValue) throws Exception {
checkExistsQuery("SELECT COUNT(*) FROM " + tableName + " where " + columnName +
" = " + columnValue),
}
private static void checkExistsQuery(Duration atMost, String query) {
try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {
await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
() -> mysqlQuery.count(query),
is(Matchers.greaterThan(0)));
}
}