I need to know if my code is a valid implementation of the factory method pattern. Also, if the Configuration and Editor were interfaces would this still be a valid implementation of the Factory Pattern?
public abstract class Configuration {
protected List<String> keyWords;
abstract void getTheme();
abstract String getKeyWords();
}
public abstract class Editor {
public final void useEditor() {
Configuration configuration = createConfiguration();
configuration.getTheme();
parse(configuration.getKeyWords());
save();
}
//The Factory Method
public abstract Configuration createConfiguration();
public abstract void parse(String sourceCode);
public void save() {
System.out.println("The file was saved..");
}
}
public class JavaConfiguration extends Configuration {
public JavaConfiguration() {
keyWords = new ArrayList<>();
keyWords.add("class");
keyWords.add("protected");
}
@Override
void getTheme() {
System.out.println("Applied Java Theme...");
}
@Override
String getKeyWords() {
return keyWords.toString();
}
}
public class PythonConfiguration extends Configuration {
public PythonConfiguration() {
keyWords = new ArrayList<>();
keyWords.add("def");
keyWords.add("print");
}
@Override
void getTheme() {
System.out.println("Applied Python Theme...");
}
@Override
String getKeyWords() {
return keyWords.toString();
}
}
public class JavaEditor extends Editor {
@Override
public Configuration createConfiguration() {
return new JavaConfiguration();
}
@Override
public void parse(String sourceCode) {
System.out.println("Parsing Java code...");
}
}
public class PythonEditor extends Editor {
@Override
public Configuration createConfiguration() {
return new PythonConfiguration();
}
@Override
public void parse(String sourceCode) {
System.out.println("Parsing python code...");
}
}
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String language = sc.next();
Editor editor = createEditor(language);
editor.useEditor();
}
private static Editor createEditor(String language) {
switch(language) {
case "Java":
return new JavaEditor();
case "Python":
return new PythonEditor();
default:
return null;
}
}
}
Edit: It seems that the Factory Method Pattern is not the appropriate design pattern to use in this case, as suggested by @200_success and @Timothy Truckle. A simple Editor class which receives a Configuration as a parameter should be enough.