I have some an abstract class A so that it has somewhich declares an abstract method doStuff currently. Currently there are many classes that inherit from A and implement doStuff.
The class' instances are initialized at run-time via AFactory based on user input.
Originally none ofall the classes needed more then thishad the same single parameter but(the user input). But now I have aan additional parameter that only a new class that inherits A needs.
So breaking it down I though of the following logic:
The interpreter class that generates instances based on user input(using
AFactoryof course) was not aware of this extra parameter.Trying to push it into the class interpreter class would be really awkward because then I would have to know when to pass it to the factory which defeats the whole purpose of having a factory in the first place.
- Sending it blindly into the Factory hoping it might do something with it seems quite ugly as well.
My current solution: Meanwhile I've decided to refactor
A.doStuff(Param param)intoA.doStuff(AParams params).
AParams can hold what ever parameters needed and doStuff can ignore then if they're not interested in them. This also seems a bit awkward to me , and remids me of sending structs in WIN32API that can hold a lot of ugly useless parameters and I'm not fond of it.
Is there a more elegant way to approach this problem ? Or some design pattern that I've overlooked and solves this?
Notes :
- We're using Java 1.7
- The class' names are silly in order to emphasize the theoretical design issue they do have normal indicative, meaningful names in reality :)
- I did search quite a lot but having figured out that it's quite hard to search the web for specific abstract theoretical issues (as opposed to why is
XthrowingExceptionin this code) I've decided to ask anyway so I'm sorry if this is a duplicate.
Edit 1:
- Clarification: I need to pass a subclass-specific argument to the
doStuffmethod.
EDIT 2:
- I did not fully understand Kilian Foth's intention so I've written some Java-pseudo-code to help me better explain the problem/understand your solution. So:
This is a skeleton of my problem.
This is a skeleton of my solution.
This is what I think might be Kilian Foth's solution, but I'm not sure.