0

I have some client classes A, B and C that need to use an operation op() that depends on the type of the caller and it's defined by another class D.

There are a few solutions:

  1. D exposes a different method for each client but the responsibility of choosing the right implementation is left to the client:
    UML
  2. overloading the method name in D and passing the client self-reference to drive the binding:
    UML
  3. introducing a strategy pattern.
  4. ...

Is there some good practice why I should avoid the second solution?

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
Tieco
  • 13
  • 2
  • 1
    *"Is there some good practice why I should avoid the second solution?"* Why do you think you should avoid it? – Tom Sep 15 '16 at 16:29
  • @Tom I don't know. That's why I'm asking... – Tieco Sep 15 '16 at 16:32
  • I'd like to see some code here. Is there a reason that `op()` in in a separate class and not just a member of the three previous classes? – markspace Sep 15 '16 at 16:36
  • [Exact need of Method Overloading in Java](http://stackoverflow.com/q/38967498) – Tom Sep 15 '16 at 16:41
  • @markspace There's no code. It's just an hypothetical situation that I've created because I'm studying for the SW engineering exam. – Tieco Sep 15 '16 at 16:47
  • @Tom in the question you linked I can't find if there's a reason to avoid passing the self reference in cases like the one I've described, even so I agree and understand what are the benefits of using the overloading – Tieco Sep 15 '16 at 17:03

0 Answers0