Reputation: 25
I am currently extending a discrete-event simulation and (coming from Python) have a hard time with some design patterns of Java.
Quick outline of what I am trying to accomplish: I want to test different preprocessing procedures in a self-written simulation framework. Each way of preprocessing contains up to several hundred lines of code.
I have created an enum class with:
public enum PreProcessorType implements preprocessorUsable {
NoPreprocessing {
@Override
someMethod()
},
SomePreprocessing {
@Override
someMethod()
},
FullPreprocessing {
@Override
someMethod()
}
}
Now all these multiple methods for each preprocessing-type have become very large, making the whole PreProcessorType class unreadable. Is there a neat possibility to put the content in a different java file? Or does this defeat the whole idea of the ("constant-specific") class body of the enum?
The initial reason that I created this preproprocessing as an enum is that I can easily iterate over it in the simulation framework, like that:
PreProcessorType[] preprocessortypes = {PreProcessorType.NoPreprocessing,
PreProcessorType.SomePreprocessing,
PreProcessorType.FullPreprocessing}
for (PreProccesorType p : preprocessortypes){
// do the preprocessing
p.someMethod();
// run the actual simulation
runSimulation();
}
An alternative would be: For each preprocessing approach, write a separate java file with the corresponding class and all necessary methods (implementing the needed interface).
Is there a way that I can safe this "enum" approach and simply store the constant-specific part of each type in a separate java file for readability?
Thank you in advance!
Please also let me know if there is a major misunderstanding regarding the design here.
Upvotes: 1
Views: 85
Reputation: 59699
You won't be able to save the enum body into different class files. You can accomplish the same thing with separate class files for each preprocessing type:
interface PreprocessorUsable {
default void someMethod() {}
}
class NoPreprocessing implements PreprocessorUsable {}
class SomePreprocessing implements PreprocessorUsable {}
class FullPreprocessing implements PreprocessorUsable {}
Then have the PreProcessorType enum just save references to the implementations.
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;
}
}
You'd use it like so:
PreProcessorType[] preprocessortypes = {PreProcessorType.NoPreprocessing,
PreProcessorType.SomePreprocessing,
PreProcessorType.FullPreprocessing};
for (PreProcessorType p : preprocessortypes){
// do the preprocessing
p.getPreprocessor().someMethod();
// run the actual simulation
runSimulation();
}
Or, use a class that just holds static constants of the different implementations.
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();
}
Now, you'd use the instances instead of an enum:
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();
}
Upvotes: 2