【问题标题】:constant-specific class bodies of enum in separate java file单独的 java 文件中枚举的常量特定类体
【发布时间】:2020-06-30 04:26:24
【问题描述】:

我目前正在扩展离散事件模拟,并且(来自 Python)很难使用 Java 的一些设计模式。

简要概述我要完成的工作: 我想在一个自写的模拟框架中测试不同的预处理程序。 每种预处理方式都包含多达数百行代码。

我创建了一个枚举类:

public enum PreProcessorType implements preprocessorUsable {

    NoPreprocessing {
        @Override
        someMethod()
    },

    SomePreprocessing {
        @Override
        someMethod()
    },

    FullPreprocessing {
        @Override
        someMethod()
    }
}

现在每个预处理类型的所有这些多个方法都变得非常大,使得整个 PreProcessorType 类不可读。是否有可能将内容放在不同的 java 文件中?或者这是否破坏了枚举的(“特定于常量”)类主体的整个想法?

我将这个预处理创建为枚举的最初原因是我可以在模拟框架中轻松地对其进行迭代,如下所示:

PreProcessorType[] preprocessortypes = {PreProcessorType.NoPreprocessing,
                                        PreProcessorType.SomePreprocessing,
                                        PreProcessorType.FullPreprocessing}

for (PreProccesorType p : preprocessortypes){
    // do the preprocessing
    p.someMethod();
    
    // run the actual simulation
    runSimulation();
}

另一种选择是: 对于每种预处理方法,编写一个单独的 java 文件,其中包含相应的类和所有必要的方法(实现所需的接口)。

有没有一种方法可以保护这种“枚举”方法,并简单地将每种类型的常量特定部分存储在单独的 java 文件中以提高可读性?

提前谢谢你!

如果对这里的设计有重大误解,也请告诉我。

【问题讨论】:

    标签: java enums


    【解决方案1】:

    您将无法将枚举主体保存到不同的类文件中。您可以为每种预处理类型使用单独的类文件来完成同样的事情:

    interface PreprocessorUsable { 
        default void someMethod() {} 
    }
    class NoPreprocessing implements PreprocessorUsable {}
    class SomePreprocessing implements PreprocessorUsable {}
    class FullPreprocessing implements PreprocessorUsable {}
    

    然后让 PreProcessorType 枚举只保存对实现的引用。

    public enum PreProcessorType {
        NoPreprocessing(new NoPreprocessing()),
        SomePreprocessing(new SomePreprocessing()),
        FullPreprocessing(new FullPreprocessing());
    
        final PreprocessorUsable preprocessor;
    
        PreProcessorType(PreprocessorUsable preprocessor) {
            this.preprocessor = preprocessor;
        }
    
        public PreprocessorUsable getPreprocessor() {
            return preprocessor;
        }
    }
    

    你会这样使用它:

    PreProcessorType[] preprocessortypes = {PreProcessorType.NoPreprocessing,
        PreProcessorType.SomePreprocessing,
        PreProcessorType.FullPreprocessing};
    
    for (PreProcessorType p : preprocessortypes){
        // do the preprocessing
        p.getPreprocessor().someMethod();
    
        // run the actual simulation
        runSimulation();
    }
    

    或者,使用仅包含不同实现的静态常量的类。

    class PreProcessorTypes {
        public static final PreprocessorUsable NO_PREPROCESSING = new NoPreprocessing();
        public static final PreprocessorUsable SOME_PREPROCESSING = new SomePreprocessing();
        public static final PreprocessorUsable FULL_PREPROCESSING = new FullPreprocessing();
    }
    

    现在,您将使用实例而不是枚举:

    PreprocessorUsable preprocessors = {PreProcessorTypes.NO_PREPROCESSING,
        PreProcessorTypes.SOME_PREPROCESSING,
        PreProcessorTypes.FULL_PREPROCESSING};
    
    for (PreprocessorUsable p : preprocessors){
        // do the preprocessing
        p.someMethod();
    
        // run the actual simulation
        runSimulation();
    }
    

    【讨论】:

    • 非常感谢您的彻底和即时回复。真的很感激!
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多