冗余代码向来是代码的一种坏味道,也是我们程序员要极力避免的。今天我通过一个示例和大家分享下解决冗余代码的3个手段,看看哪个最好。
创新互联专业为企业提供天峨网站建设、天峨做网站、天峨网站设计、天峨网站制作等企业网站建设、网页设计与制作、天峨企业网站模板建站服务,10多年天峨做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
为了描述这个问题,我将使用 FtpClient 作为示例。要从 ftp 服务器获取一些文件,你需要先建立连接,下一步是登录,然后执行查看ftp文件列表、删除ftp文件,最后注销并断开连接, 代码如下:
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);
}
}
}
public boolean deleteFile(String filePath) {
try {
ftpClient.connect("host", 22);
ftpClient.login("username", "password");
return ftpClient.deleteFile(filePath);
} 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);
}
}
}
}
正如上面代码所示,listDirectories和downloadFtpFile中都包含了ftp连接、登录以及最后的注销操作,存在大量冗余的代码,那有什么更好的办法清理冗余代码呢?下面推荐3个做法,所有三个提出的解决方案都将实现以下 FtpProvider 接口,我将比较这些实现并选择更好的一个。
public interface FtpProvider {
FTPFile[] listDirectories(String directory) throws IOException;
boolean deleteFile(String filePath) throws IOException;
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FtpOperation {
}
@Slf4j
@Service
class FtpProviderImpl implements FtpProvider {
private final FTPClient ftpClient;
@Override
public FTPFile[] listDirectories(String directory) throws IOException {
return ftpClient.listDirectories(directory);
}
@Override
public boolean deleteFile(String filePath) throws IOException {
return ftpClient.deleteFile(filePath);
}
}
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class FtpOperationProxy {
private final FTPClient ftpClient;
@Around("@annotation(daniel.zielinski.redundancy.proxyaop.infrastructure.FtpOperation)")
public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {
try {
ftpClient.connect("host", 22);
ftpClient.login("username", "password");
return joinPoint.proceed();
} 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);
}
}
}
}
所有用@FtpOperation 注解的方法都会在这个地方执行joinPoint.proceed()。
@FunctionalInterface
interface FtpOperation{
R apply(T t) throws IOException;
}
@RequiredArgsConstructor
@Slf4j
@Service
public class FtpOperationTemplate {
private final FTPClient ftpClient;
publicK execute(FtpOperation ftpOperation) {
try {
ftpClient.connect("host", 22);
ftpClient.login("username", "password");
return ftpOperation.apply(ftpClient);
} 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);
}
}
}
}
@RequiredArgsConstructor
@Slf4j
@Service
class FtpProviderFunctionalInterfaceImpl implements FtpProvider {
private final FtpOperationTemplate ftpOperationTemplate;
public FTPFile[] listDirectories(String parentDirectory) {
return ftpOperationTemplate.execute(ftpClient -> ftpClient.listDirectories(parentDirectory));
}
public boolean deleteFile(String filePath) {
return ftpOperationTemplate.execute(ftpClient -> ftpClient.deleteFile(filePath));
}
}
我们正在 FtpOperationTemplate 上执行方法 execute 并且我们正在传递 lambda 表达式。我们将放入 lambda 中的所有逻辑都将代替 ftpOperation.apply(ftpClient) 函数执行。
@RequiredArgsConstructor
@Slf4j
@Service
abstract class FtpOperationTemplate{
protected abstract K command(FTPClient ftpClient, T input) throws IOException;
public K execute(FTPClient ftpClient, T input) {
try {
ftpClient.connect("host", 22);
ftpClient.login("username", "password");
return command(ftpClient, input);
} 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);
}
}
}
}
@Slf4j
@Service
class FtpOperationListDirectories extends FtpOperationTemplate{
@Override
protected FTPFile[] command(FTPClient ftpClient, String input) throws IOException {
return ftpClient.listDirectories(input);
}
}
@Slf4j
@Service
class FtpOperationDeleteFile extends FtpOperationTemplate{
@Override
protected Boolean command(FTPClient ftpClient, String input) throws IOException {
return ftpClient.deleteFile(input);
}
}
@RequiredArgsConstructor
@Slf4j
@Service
public class FtpProviderTemplateImpl implements FtpProvider {
private final FtpOperationTemplateftpOperationListDirectories;
private final FtpOperationTemplateftpOperationDeleteFile;
private final FTPClient ftpClient;
public FTPFile[] listDirectories(String parentDirectory) {
return ftpOperationListDirectories.execute(ftpClient, parentDirectory);
}
public boolean deleteFile(String filePath) {
return ftpOperationDeleteFile.execute(ftpClient, filePath);
}
}
我们正在 FtpOperationTemplate 上执行方法 execute 并在那里传递我们的参数。因此执行方法的逻辑对于 FtpOperationTemplate 的每个实现都是不同的。
我们现在来比较下上面种方式:
向 FtpProvider 接口添加一个新方法,需要我们仅在一个地方进行更改。我们可以轻松地将我们的 FtpProvider 注入到其他服务中。此解决方案的强项可能是 @FtpOperation 注释,它可以在 FtpProvider 上下文实现之外使用,但是将 Ftp 操作的逻辑划分到单独的类中并不是一个好方法。
向接口 FtpProvider 添加一个新方法,需要我们仅在一个地方进行更改。我们可以轻松地将我们的 FtpProvider 注入到其他服务中。我们将ftp操作的逻辑封装在一个类中。相对于上面的方式,我们也没有用到AOP的库,所以我个人还是比较推荐的。
向接口 FtpProvider 添加一个新方法,需要我们在两个地方进行更改。我们需要添加一个新的类,会导致类爆炸,另外,我们还需要将实现注入到 FtpProvider。
如果是你,你会选择哪种方式呢?还是有更好的方法?
网页题目:解决冗余代码的三种方法,让你的代码更上一层楼
文章地址:http://www.gawzjz.com/qtweb/news29/191729.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联