Member-only story
Remove redundancy in your implementation - AOP Proxy vs Functional Interface vs Template Design Pattern.
6 min readFeb 27, 2023
The problem of redundancy is known to every programmer. Below I will present the redundant code and its exemplary solutions. Let me know if I helped you to arrange your code a little better.
Problem description
To describe the problem I will use as an example FtpClient. To fetch some file from ftp server you need first make a connection, next step is to login, then you can execute download command, at the end is good to logout and disconnect. Check how it can be implemented in Java.
public class FtpProvider{
private final FTPClient ftpClient;
public FTPFile[] listDirectories(String parentDirectory) {
try {
ftpClient.connect("host", 22);
ftpClient.login("username", "password");
return ftpClient.listDirectories(parentDirectory);
} catch (IOException ex) {
log.error("Something went wrong", ex);
throw new RuntimeException(ex);
} finally {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException ex) {
log.error("Something went wrong while finally", ex);
}
}
}
}