Refactoring methods that use the same code but different types

后端 未结 5 1836
情深已故
情深已故 2020-12-31 14:22

I have several methods that do the same thing yet, when interfacing with the MySQL database, save or load a different type of parameter. Currently, I have a different method

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 15:00

    What if you broke out your comparative functionality and had your methods down to the most granular level? For example:

    public static void update(Connection con, int playerID, String tableName, String fieldName, String value) {
        // update query logic here
    }
    

    And the same for delete(). There is no reason to pass both the "new" and "original" values into this function and do the compare inside. I suggest looping through the arrays, comparing, and calling either update() or delete() based on your needs. To deal with different data types, I would always pass in the String value of what you want in the database.

提交回复
热议问题