Seria ruim usar o Strategy com Factory para resolver este exercício?
package Factory;
import java.sql.Connection;
public class TestaConnectionFactory {
public static void main(String[] args) {
Connection conexao = new ConnectionFactory().getConnection(new MySQL());
}
}
package Factory;
import java.sql.Connection;
public class ConnectionFactory {
public Connection getConnection(BancoDeDados banco){//renomear para get
return banco.getConnection();
}
}
package Factory;
import java.sql.Connection;
public interface BancoDeDados {
Connection getConnection();
}
package Factory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQL implements BancoDeDados {
@Override
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/banco", "root", "123");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package Factory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Outro implements BancoDeDados {
@Override
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:outrobanco://localhost:3306/banco", "root", "123");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}