Java Design Patterns – Refactoring an Existing Abstract Class and Its Parameters

abstract classabstractiondesigndesign-patternsjava

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 AFactory of 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) into A.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 X throwing Exception in 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 doStuff method.

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.

Best Answer

The point of a factory is to hide the fact that you may get objects from slightly different classes. Therefore, if some of these objects need certain data and others don't...

  • Either they aren't similar enough to be produced by the same factory. Then you need to have more than one factory and make the decision way earlier than you are currently doing.

  • Or they are similar enough that the client isn't supposed to care which one they get. Then you can't expect the client to care about whether to send the extra data or not. It follows that they must always be passed, but the factory will sometimes ignore them without bothering the client with that implementation detail. Anything else would burden the client with the very distinction that the factory was supposed to conceal.

Related Topic