简体   繁体   English

如何避免由于代码重复而导致的重载?

[英]How to avoid overloading due to code duplication?

I am trying to insert the name,hashed password,salt and the hash type to the database. 我试图将名称,哈希密码,盐和哈希类型插入数据库。 Only thing changing is the type of the parameters. 唯一改变的是参数的类型。 I believe that it can be done more efficiently. 我相信可以更有效地做到这一点。 How can I avoid using overloading? 如何避免使用重载? Do I need to use generics? 我需要使用泛型吗? Thank you. 谢谢。

InsertMethods 插入方法

protected void insert(String name, String secretpassword, String salt, String type)
{
    String sql = "INSERT INTO login(username,password,salt,type) VALUES(?,?,?,?)";

    try (Connection conn = this.connect();
         PreparedStatement pstmt = conn.prepareStatement(sql)) {
        pstmt.setString(1, name);
        pstmt.setString(2, secretpassword);
        pstmt.setString(3, salt);
        pstmt.setString(4, type);
        pstmt.executeUpdate();
        System.out.println("Successful");
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

protected void insert(String name, byte[] secretpassword, String salt, String type)
{
    String sql = "INSERT INTO login(username,password,salt,type) VALUES(?,?,?,?)";

    try (Connection conn = this.connect();
         PreparedStatement pstmt = conn.prepareStatement(sql)) {
        pstmt.setString(1, name);
        pstmt.setString(2, Arrays.toString(secretpassword));
        pstmt.setString(3, salt);
        pstmt.setString(4, type);
        pstmt.executeUpdate();
        System.out.println("Successful");
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

You can call the first method from the second one like: 您可以从第二个方法中调用第一个方法,例如:

protected void insert(String name, byte[] secretpassword, String salt, String type)
{
    insert(name, Arrays.toString(secretpassword), salt, type);
}

I don't see much of an issue here, because you always end up inserting the password as a string into your login table. 我在这里看不到很多问题,因为您总是最终将密码作为字符串插入到login表中。 So, I suggest just always using the first version: 因此,我建议始终使用第一个版本:

protected void insert(String name, String secretpassword, String salt,
    String type);

If you come across your password as a byte[] , just use string's constructor to generate a String : 如果您以byte[]的形式输入密码,只需使用string的构造函数生成String

byte[] array = ...;
String password = new String(array);

So, to be clear, I suggest possibly deleting the second method, because other than one parameter, it does precisely the same thing at the database level. 因此,很明显,我建议可能删除第二种方法,因为除了一个参数外,它在数据库级别上的作用完全相同。

Keeping both methods means more maintenance work in your code base, because if you change one, you will have to make the same logical changes in the other. 保留这两种方法意味着在代码库中进行更多的维护工作,因为如果更改一种方法,则必须在另一种方法中进行相同的逻辑更改。

Two ways come to mind: 我想到两种方法:

public String getValue(String value1) {
  return value1 + "0";
}

public String getValue(String value1, int num) {
  return value1 + String.valueOf(num);
}

Option 1: extract the code in a method that both call: 选项1:在同时调用以下方法的方法中提取代码:

public String getValue(String value1) {
  return getMyValue(value1, 0);
}

public String getValue(String value1, int num) {
  return getMyValue(value1, num);
}

public String getMyValue(String val, int num) {
  return val + String.valueOf(num);
}

or, option 2, chain the calls: 或者,选项2,将通话链接:

public String getValue(String value1) {
  return getValue(value1, 0);
}

public String getValue(String value1, int num) {
  return value1 + String.valueOf(num);
}

but, in your case, you should really ask yourself whether you need both methods. 但是,在您的情况下,您应该真正问自己是否需要两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM