I have an abstract class A which declares an abstract method doStuff. 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 all the classes had the same single parameter (the user input). But now I have an 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).AParamscan hold what ever parameters needed anddoStuffcan 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:
doStuffmethod?EntryPointlet's call itmainwhich then invokes someInterpreterwhich currently does not receive the extra param as a parameter and so does the factory. The extra param comes from another type of user input that is not described byUserInput(which unfortunately I can not change).