I think I have a migraine coming on already.
I would like to use polymorphism to create a factory. I will create an interface and 2 separate classes to implement it. My main factory class will be MyFactory and my builder class will be FactoryManager.
Interface:
IFactoryFitout
Classes:
FitOut1
FitOut2
Factory Class:
MyFactory(IFactoryFitout fitOut) {
}
Builder Class:
FactoryManager
I can do the following:
MyFactory myFactory = new MyFactory(new FitOut1);
But how do I do it the following way, properly?:
i = getRnd(2);
actions = dictionary();
MyFactory myFactory = new MyFactory(actions.get(i));
This is what I have worked out on my own but something tells me I have the wrong approach. There is something about calling methods from a HashMap that is unsettling. Also im not sure if the code for the HashMap would be able to be placed in a separate file, which also seems to break the open close principle. Can someone please tell me if I am taking the correct approach or provide me with a better method. Thank you in advance....
What I have tried:
HashMap<string,> actions;
private HashMap<integer,> dictionary() {
try {
actions.put(0, this.getClass().getMethod("createNewObjects"));
actions.put(1, this.getClass().getMethod("modifyExistingObjects"));
}
return dictionary;
}
public void builder() {
Int arg = getRandom(2);
actions = dictionary();
if (actions.containsKey(arg)) {
try {
actions.get(arg).invoke();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
I didn't bother adding the rest of this code because im sure you can tell this is heading directly for a brick wall. :/ This would probably work but does not take advantage of interfaces.