java - 创建文件并返回枚举作为结果的模式或约定

标签 java design-patterns convention

场景
我有一个 util 类,它可以从文件夹创建 zip 文件并返回该文件。这很完美。

public static class MakeZip {
    // returns zip file, null otherwise
    public static File doZip(File folderToZip)
}

问题
当我想控制可能的错误场景、连接问题、空文件夹、zip 失败等时出现...我想知道创建文件并返回的最佳方法结果,为此,我创建了一个枚举

public static enum ZIP_RESULT {
    OK, KO, EMPTY_FOLDER;
}

想法
a) 创建文件类属性

private File zipFile; 

并在 doZip 方法中返回 ZIP_FILE 枚举。

public ZIP_RESULT doZip(File folderToZip)

b)修改发送的文件:

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.

关于java - 创建文件并返回枚举作为结果的模式或约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192828/

相关文章:

events - 信号和槽 vs. 事件和事件监听器

variables - 哪个变量名合适?

java - Java 中的 Post 请求参数不起作用

java - JTable 单元格颜色随事件变化

java - 在 Java 中用原语规避代码膨胀的模式

java - 没有函数指针,除了接口(interface)之外,在 Java 中实现回调的推荐方法是什么?

openstreetmap - 室内地图创建器

Python 构造函数约定

java - 使用 TextView 获取空指针

java - 如何在内部控制中传递 ObservableField