创建文件的模式或约定,结果为 return 枚举
Pattern or convention to create a file and return enum as result
SCENARIO
我有一个实用程序 class 可以从文件夹制作一个 zip 文件,return 是该文件。这很完美。
public static class MakeZip {
// returns zip file, null otherwise
public static File doZip(File folderToZip)
}
问题
当我想控制可能的错误场景、连接问题、空文件夹、压缩失败等时出现了...我想知道最好的方法为了创建文件和 return 结果,为此,我创建了一个枚举
public static enum ZIP_RESULT {
OK, KO, EMPTY_FOLDER;
}
想法
a)创建文件class属性
private File zipFile;
和return ZIP_FILE
doZip 方法中的枚举。
public ZIP_RESULT doZip(File folderToZip)
b)修改File
发送:
public ZIP_RESULT doZip(File folderToZip, File fileToZip)
c) ????
问题
对于这种期望枚举结果的文件修改,有模式、约定或最佳实践吗?我想这是一个非常典型的场景...
抛出异常。您可以扩展 Exception
或实施 Throwable
.
关于何时使用异常的第三个项目符号描述了您的情况here:
Exceptions due to resource failures: Exceptions that get generated
when resources fail. For example: the system runs out of memory or a
network connection fails. The client's response to resource failures
is context-driven. The client can retry the operation after some time
or just log the resource failure and bring the application to a halt.
这就是我要做的,
而不是这个:
public static File doZip(File folderToZip)
并改用类似这样的东西
public static Map.Entry<File,ZIP_RESULT> doZip(File folderToZip)
所以您将在 Entry
中获得这两个数据,您可以根据需要使用它们。
SCENARIO
我有一个实用程序 class 可以从文件夹制作一个 zip 文件,return 是该文件。这很完美。
public static class MakeZip {
// returns zip file, null otherwise
public static File doZip(File folderToZip)
}
问题
当我想控制可能的错误场景、连接问题、空文件夹、压缩失败等时出现了...我想知道最好的方法为了创建文件和 return 结果,为此,我创建了一个枚举
public static enum ZIP_RESULT {
OK, KO, EMPTY_FOLDER;
}
想法
a)创建文件class属性
private File zipFile;
和return ZIP_FILE
doZip 方法中的枚举。
public ZIP_RESULT doZip(File folderToZip)
b)修改File
发送:
public ZIP_RESULT doZip(File folderToZip, File fileToZip)
c) ????
问题
对于这种期望枚举结果的文件修改,有模式、约定或最佳实践吗?我想这是一个非常典型的场景...
抛出异常。您可以扩展 Exception
或实施 Throwable
.
关于何时使用异常的第三个项目符号描述了您的情况here:
Exceptions due to resource failures: Exceptions that get generated when resources fail. For example: the system runs out of memory or a network connection fails. The client's response to resource failures is context-driven. The client can retry the operation after some time or just log the resource failure and bring the application to a halt.
这就是我要做的,
而不是这个:
public static File doZip(File folderToZip)
并改用类似这样的东西
public static Map.Entry<File,ZIP_RESULT> doZip(File folderToZip)
所以您将在 Entry
中获得这两个数据,您可以根据需要使用它们。