【问题标题】:Java code reuse in two or more methods with same code使用相同代码在两个或多个方法中重用 Java 代码
【发布时间】:2018-09-27 13:46:13
【问题描述】:

我在 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 中,我大部分是重复代码。

编辑:忘了说,他们最后可能有不同的断言,例如:

是(Matchers.greaterThan(0)));

是(Matchers.equalTo(countValueExpected)));

【问题讨论】:

  • 将查询作为参数传递给您的函数
  • @Codeer 我同意你的看法,但只有在用户未提供值的情况下才可以接受(仍然不推荐)。
  • @killjoy 绝对正确。我暂时忘记了这一点

标签: java oop design-patterns code-reuse


【解决方案1】:

如果我没看错的话,这些方法只在您提供给count() 的参数上有所不同。只需引入一个将其作为参数的方法,并使用不同的值调用它。

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)));
    }
}

【讨论】:

  • 我已经给你答案了。只是一个小改动 - 你能在问题底部看到我的编辑吗?
  • @Saffik 只需添加另一个 Matcher 类型的参数。
【解决方案2】:

您可以在单独的方法中简单地提取常见行为:

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)));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多