greenHorn
greenHorn

Reputation: 557

how the instanceof usage can be avoided?

How can we avoid using instanceof operator for writing a clean code say for example: If an object is of Type1 do something, but if it is of Type2 then do something else.

if(obj instanceof T1){
    doSomething()
 }else(obj instanceof T2){
    doOtherThing()
 }

Upvotes: 2

Views: 235

Answers (3)

davidxxx
davidxxx

Reputation: 131346

Generally you use a series of instanceof with a cast to invoke a specific method that exists in a class but not in others :

if(obj instanceof T1){
    ((T1)obj) doSomething();
}
else(obj instanceof T2){
    ((T2)obj) doOtherThing();
}

To avoid that, if you can modify these classes, T1 and T2 should be subclasses of a common base class and the invoked method should be defined in the base class.
In this way, each subclass may implement it and you could so write :

obj.doSomething();

if you cannot modify these classes, you can always introduce an Adapter class that wraps them to provide a common way to invoke these methods.

Adapter

public interface Adapter{
    void process();
}

AdapterForT1

public class AdapterForT1 implements MyAdapter{

    private T1 adapted;
    public AdapterForT1(T1 adapted){
      this.adapted = adapted;
    } 

    public void process(){
       adapted.doSomething();
    }

}

AdapterForT2

public class AdapterForT2 implements MyAdapter{

    private T2 adapted;
    public AdapterForT2(T2 adapted){
      this.adapted = adapted;
    } 

    public void process(){
       adapted.doOtherThing();
    }

}

And you may use them in this way :

MyAdapter adapter = new AdapterForT1(obj);
...
adapter.process();

or :

MyAdapter adapter = new AdapterForT2(obj);
...
adapter.process();

Upvotes: 3

QBrute
QBrute

Reputation: 4536

Create an interface, which both classes implement. An interface is sort of a contract that every implementing class has to comply with.

With this you can ensure that every instance of such a class has all defined methods from that interface implemented.

For example:

public interface CustomExecutable {
    void execute();
}

Then your two classes:

public class T1 implements CustomExecutable {
    @Override
    public void execute() {
        // Do t1 logic
    }
}

public class T2 implements CustomExecutable {
    @Override
    public void execute() {
        // Do t2 logic
    }
}

In your main program you could do something like this:

CustomExecutable ce = new T1();
ce.execute(); // Does t1 logic
ce = new T2();
ce.execute(); // Does t2 logic

instanceof isn't necessary anymore, because each type has its own way of executeing its code.

Upvotes: 1

Aman Chhabra
Aman Chhabra

Reputation: 3894

You need to use method overriding here and then call the same function from different instances of different classes which will execute a different set of instructions as per implemented in individual classes

So for instance, you have a class T and then class T1 and class T2 both extends class T and all three classes implement executeTask function

So,

T t1 = new T1();
T t2 = new T2();

// this will execute function overridden in T1
t1.executeTask();

// this will execute function overridden in T2
t2.executeTask();

Upvotes: 1

Related Questions