Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 9-May-16 3:49am
v2
Comments
Sergey Alexandrovich Kryukov 9-May-16 9:20am    
Please, what are you talking about? If you use polymorphism, you need to have a set of polymorphic type with at least two different types implementing at least one common interface. The use of a factory cannot change it. You show zero types and zero interfaces.

Also, you hard-code "createNewObjects" and "modifyExistingObjects"... I would say, this is the death of any factory idea, or any other idea. The role of random... It's either unclear, or... I'm just afraid to guess. :-)

—SA
Michael Hurt 9-May-16 9:28am    
Yeah, I had trouble trying to get it clear in my own head. I'll try re-writing it.
Michael Hurt 9-May-16 10:06am    
This will be used to randomly create or edit/delete objects. The objects will be stored in a list which will have a limit of 10 objects. The effect will be that the objects will have a random life cycle.
Michael Hurt 9-May-16 10:47am    
I think I may have just found the answer.
http://www.java2s.com/Tutorial/Java/0100__Class-Definition/Polymorphism.htm

Would that be the best approach?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900