Skip to main content
Code Review

Return to Answer

added 4 characters in body
Source Link
Roman
  • 2.9k
  • 11
  • 23

There are two benefits that are in my mind. The first is the short parameter list and the second one is that you could have multiple types of credentials:

There two benefits that are in my mind. The first is the short parameter list and the second one is that you could have multiple types of credentials:

There are two benefits that are in my mind. The first is the short parameter list and the second one is that you could have multiple types of credentials:

Source Link
Roman
  • 2.9k
  • 11
  • 23

You can decide between to structural design patterns:

I would go with the Template Method Pattern because you do not reuse the algorithm which is a benefit of the Strategy Pattern.

In the first step we need to create an abstract class which has all the common code and a invocation of an abstract method where the algorithms distinguish.

abstract class PersistentAction {
 private final Connection connection;
 private final String username;
 private final String password;
 
 PersistentAction(Connection connection, String username, String password) { /* ... */ }
 final boolean execute(String query) {
 try(PreparedStatement statement = connection.prepareStatement(query)) {
 statement.setString(1, credential.getUsername());
 statement.setString(2, credential.getPassword());
 ResultSet result = statement.executeQuery();
 return evaluate(result);
 } catch(SQLException e) {
 return false;
 }
 }
 abstract boolean evaluate(Result result);
}

After that we can create our algorithms which extends from our abstract class and implement the abstract method.

class HasNextEvaluation extends PersistentAction {
 @Override
 protected boolean evaluate(ResultSet result) {
 return result.next();
 }
}
class ConstantTrue extends PersistentAction {
 @Override
 protected boolean evaluate(ResultSet result) {
 return true;
 }
}

After the two steps we can achieve the following:

public class DatabaseHandler {
 private static final Connection connection = createConenction();
 
 public static boolean checkLogin(String username, String password) {
 String query = "select * from users where username = ? and password = ? ";
 
 PersistentAction action = new HasNextEvaluation(connection, username, password);
 return action.execute(query);
 }
 
 public static boolean registerUser(String username, String password) {
 String query = "select * from users where username = ? and password = ? ";
 
 PersistentAction action = new ConstantTrue(connection, username, password);
 return action.execute(query);
 }
}

From here I would improve the parameter list checkLogin(String username, String password) by using an Paramter Object.

public static boolean checkLogin(Credential credential) {
 /* ... */
}

There two benefits that are in my mind. The first is the short parameter list and the second one is that you could have multiple types of credentials:

  • username and password
  • email and password
  • mobile number
  • (even with biometrics πŸ‘€)
default

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /