I'm working on an android application, but this is basically plain 'ole java.
The aim is to do the right thing and not repeat myself.
public enum Datafile {
ACTIVITIES() {
@Override public String filepath() {
return "activities.yaml";
}
},
OPEN_LOOPS() {
@Override public String filepath() {
return "open_loops.yaml";
}
};
public abstract String filepath();
}
Later on, when I want to edit the file I try this
File file = getDataFile(Datafile.OPEN_LOOPS);
Which calls this...
private File getDataFile(Datafile datafile) {
File file = null;
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
file = new File(baseDir + "/" + datafile.filepath());
return file;
}
Points for a better name for the enum and good advice regarding who responsibility for the getFileData method (currently all this code is in an Activity) really lies (a new class).