I have a set of java subclasses which all extend the same superclass. They contain exactly the same methods and make the same computations on input data of different type. That means that one is responsible to deal with int, another with double, another with int Array, List etc. Objects of each type should be created at runtime of the program according to the input data each time.
What is the best way to specify what kind of object (of the referred subclasses) will be created each time while the program is running? It seems not possible to use something like type checking since the data could be of different type (class).
Do I need a separate class that is responsible exclusively for this task, something that serves as an object generator? If so, is there a hint available about what would be the form and functionality of such a class?
Thank you.
I’m not sure I understood your question correctly as it’s not very clear, some examples of your class and of what you trying to accomplish would help.
Anyway, what I got from it is that you should check the Visitor pattern.
http://en.wikipedia.org/wiki/Visitor_pattern
In simpler words, I would separate the actions you want to make with each type, and have one class for each one of these (instead of having all these as methods in all your subclasses).
And you would do something like that:
A visit method would be defined on the Visitor interface, once for each type of Subclass you have. And each of your action class would have to implement what to do for each one of these. And the accept method, in your superclass, would just call visit from the action.
If this is not what you asked, sorry 🙂