patternjavaMinor
Configuring an editor for various languages using Factory Method Pattern
Viewed 0 times
methodlanguagesfactoryeditorconfiguringforusingvariouspattern
Problem
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 class PythonEditor extends Editor {
@Override
public Configuration createConfiguration() {
return new
public abstract class Configuration {
protected List 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
Solution
I need to know if my code is a valid implementation of the factory method pattern.
No.
The reason is that the factory cannot be part of the objects to be created. I think you would be surprised to find a factory in the the hedge of your car, wouldn't you?
Generally spoken your approach fails the Separation of Concerns principle.
You have two responsibilities in your code that belong to a factory:
Both should be in a separate factory class.
Also: your "factory" is producing the wrong type of objects because of your misconception of inheritance.
We employ inheritance when the child classes differ in behavior. But your
So what your (yet to build) factory should do is creating the "language depended" key word list and pass that into a new instance of the one and only
No.
The reason is that the factory cannot be part of the objects to be created. I think you would be surprised to find a factory in the the hedge of your car, wouldn't you?
Generally spoken your approach fails the Separation of Concerns principle.
You have two responsibilities in your code that belong to a factory:
- identifying the requested type.
- creating the actual object.
Both should be in a separate factory class.
Also: your "factory" is producing the wrong type of objects because of your misconception of inheritance.
We employ inheritance when the child classes differ in behavior. But your
Editor extensions only differ in configuration (as far as your code example is showing...).So what your (yet to build) factory should do is creating the "language depended" key word list and pass that into a new instance of the one and only
Editor class.Context
StackExchange Code Review Q#157875, answer score: 3
Revisions (0)
No revisions yet.