用例:
我正在创建一个执行hadoop文件系统操作的Java API。应用程序使用此API执行文件操作。
我创建了一个接口,它包含抽象方法和具有实现的相应实现类。现在我想将这些方法公开给应用程序开发人员,以便开发人员执行操作。
问题:
可以用来为用例提供服务的最佳设计模式是什么?
接口:HDFSClient
package com.hdfsoperations.filesystemapi;
import org.apache.hadoop.fs.Path;
public interface HDFSClient {
public void createDir(Path dir) throws Exception;
public void deleteDir(Path dir) throws Exception;
public boolean isDir(Path path);
public boolean isFile(Path path);
public void copyFromLocal(Path localpath, Path hdfspath) throws Exception;
public void copyToLocal(Path hdfspath, Path localpath) throws Exception;
public void move(Path source, Path destination) throws Exception;
}
实现类:HDFSClientImpl
package com.hdfsoperations.filesystemapi;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;
public class HDFSClientImpl implements HDFSClient {
private static final Logger LOGGER = Logger.getLogger(HDFSClientImpl.class);
private static Configuration conf = new Configuration();
FileSystem fs = null;
public void createDir(Path dir) throws Exception {
boolean res = false;
fs = FileSystem.get(conf);
res = fs.mkdirs(dir);
if (!res) {
LOGGER.info("Could not create the directory : " + dir);
} else {
LOGGER.error("Created the directory : " + dir);
}
}
public void deleteDir(Path dir) throws Exception {
boolean res = false;
fs = FileSystem.get(conf);
res = fs.delete(dir, true);
if (!res) {
LOGGER.info("Could not delete the file : " + dir);
} else {
LOGGER.error("Deleted the directory : " + dir);
}
}
public boolean isDir(Path path) {
boolean res = false;
try {
fs = FileSystem.get(conf);
res = fs.isDirectory(path);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return res;
}
public boolean isFile(Path path) {
boolean res = false;
try {
fs = FileSystem.get(conf);
res = fs.isFile(path);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
return res;
}
public void copyFromLocal(Path localpath, Path hdfspath) throws Exception {
fs = FileSystem.get(conf);
fs.copyFromLocalFile(localpath, hdfspath);
}
public void copyToLocal(Path hdfspath, Path localpath) throws Exception {
fs = FileSystem.get(conf);
fs.copyToLocalFile(hdfspath, localpath);
}
public void move(Path source, Path destination) throws Exception {
fs = FileSystem.get(conf);
fs.moveFromLocalFile(source, destination);
}
}