0

suppose I have some entities classes hierarchy , and I have a util class with a method that get the upper class hierarchy and has to to some change in another entity class (out of the previous hierarchy) according to the specific class that I got . So I will use instanceof in order to find what specific object I got. But I know : Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself. so how can I do it without using instanceof ?

Please notice that I do not want to put the util method in the entities classes because I want the users (the implementers programmers) to be able to implement the method but I do not want them to be able to change the entities.

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
user5157427
  • 241
  • 2
  • 7
  • 17
  • You might consider having all of the "candidate" classes derive from a single class or interface which defines a getType() method, possibly returning an enum type with the possible options. Bonus: you can use that enum in a switch() {} block, more performant... this pattern is very common. – BadZen Aug 08 '15 at 15:51
  • By using ___polymorphism___. That's why it's there. – Emil Laine Aug 08 '15 at 15:51
  • If for some reason you don't want to use polymorphism and want to avoid instanceof at the same time, then maybe visitor pattern could be useful? – slnowak Aug 08 '15 at 15:59
  • Please notice that I do not want to put the util method in the entities classes because I want the users (the implementers programmers) to be able to implement the method but I do not want them to be able to change the entities. – user5157427 Aug 08 '15 at 16:19

2 Answers2

3

Instead of this:

// BAD:
public void doStuff(Parent p) {
    if (p instanceof Child1)
        doThis();
    else if (p instanceof Child2)
        doThat();
    else if …
}

do this:

// GOOD:
public class Parent {
    public void doIt();
}

public class Child1 extends Parent {
    @Override
    public void doIt() { /* the contents of doThis */ }
}

public class Child2 extends Parent {
    @Override
    public void doIt() { /* the contents of doThat */  }
}

public void doStuff(Parent p) {
    p.doIt();
}

(The code may not be 100% correct (my Java is a bit rusty) but I hope you get the idea.)

This is called polymorphism.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

Method overloading is something you might wanna check out.

  • Pretty sure OP is not asking about basic method override. – BadZen Aug 08 '15 at 15:53
  • @BadZen Pretty sure that's the correct solution to OP's problem even though they're not asking about it. – Emil Laine Aug 08 '15 at 15:55
  • I might have misunderstood but from what I read, OP wants to do something with different instances of a class, so for example he can overload method `doSomething(type1 obj)` and `doSomething(type2 obj)` and add different functionallity to different instances. – Miroslav Lalev Aug 08 '15 at 15:56