Java接口 - 普通接口中的常量和静态类

Ran*_*ion 5 java

考虑以下界面.

public interface ThirdPartyApiHandler {

    public OperationResult doOperation(OperationInput input);

    public static class OperationResult {
         //members of OpeationResult. metrics after file processing
         private int successfulRecords;
         private int failedRecords;
    }  

    public static class OperationInput {
         //implementations call third party API to process this file.
         private String inputBatchFile; 
    }  

    //Constant which would be same across all implementations.
    public static final int GLOBAL_CONSTANT = 1;
}
Run Code Online (Sandbox Code Playgroud)

界面设计不好吗?

  1. OperationResultOperationInput定义为静态类.它们仅用于实现而不是其他任何地方.我在这里看到的优点是 - 我不必为这两个类创建单独的文件.他们也获得了父类的命名空间.

  2. 我已阅读有关常量接口的信息.但在这种情况下,我在普通接口中定义常量,这些常量在所有实现中都必须相同,并且将在这些实现中使用.

我是第一次使用这种模式,所以想得到建议.

Tal*_*ala 4

OperationResult 和OperationInput 被定义为静态内部类。 它们不会在其他地方使用。

没关系,因为它们不会在其他地方使用。如果它们比我更愿意让它们分在不同的班级。

我读过有关常量接口的内容。但在这种情况下,我在普通接口中定义常量,这些常量在所有实现中都必须相同,并且将在这些实现中使用。

这是声明这样一个字段的好地方。