0

Assuming the following situation:

public interface A {
    void a1();
    void a2();
}

public interface B {
    void b1(A a);
}

public class ImplA implements A {
    // interface methods
    void a1() {};
    void a2() {};
    // ImplA specific methods
    void a3() {};
}

public class ImplB implements B {
    void b1(A a) {
       if(a instaceof ImplA) { // avoid instanceof test and cast 
          ((ImplA)a).a3(); 
       }
    }
}

Would it be possible by some architectural voodoo to avoid an instanceof check in ImplB.b1()? ImplA and ImplB are in the same package and closely related to each other. I read somewhere, that the use of instanceof is a hint for a "no-so-good" interface design. Any recommendations? Thanks alot!

6
  • 1
    It's hard to say without a lot more context. Commented May 8, 2014 at 6:39
  • What kind of additional information do you need? Commented May 8, 2014 at 6:43
  • possible duplicate of Avoiding instanceof in Java Commented May 8, 2014 at 7:00
  • @Raedwald I read this, but I couldn't map the described problem on my case. Commented May 8, 2014 at 7:34
  • To avoid being closed as a duplicate, you need to explain in your question why your question is not actually a duplicate of similar questions. You accepted an answer recommending the Visitor design pattern. Which is also one of the highly voted answers of the Avoiding instanceof in Java question. Commented May 8, 2014 at 11:28

2 Answers 2

1

You could use a Visitor pattern to avoid the instanceof cast

public interface Visitor {
    void visitImplA(ImplA toVisit);
}

public class VisitorImpl implements Visitor {

    @Override
    public void visitImplA(ImplA toVisit) {
        toVisit.a3();
    }
}

public interface A {
    void a1();
    void a2();
    void accept(Visitor visitor);
}

public interface B {
    void b1(A a);
}

public class ImplA implements A {
    // interface methods
    void a1() {};
    void a2() {};
    // ImplA specific methods
    void a3() {};

    void accept(Visitor visitor) {
        visitor.visitImplA(this);
    }
}

public class ImplB implements B {
    void b1(A a) {
        a.accept(new VisitorImpl());
    }
}

This would eliminate all you instanceof checks and divide them into the visitor and the implementing classes, this pattern would suffice in the case where you'd be doing the same stuff after most of the instanceof checks, otherwise you'd need a lot of implementations of the Visitor interface

Sign up to request clarification or add additional context in comments.

3 Comments

That sounds reasonable. I will have a deeper look into that. Thanks.
It seems to me that this pattern is better used, when ImplA and ImplB don't know of each other and the goal is to keep it that way. In my case, they are both closely related to each other (through composition).
yeah. Well the easiest way to avoid the instanceof check is to pass an ImplA object in the same place or ad the a3() to the interface, but these are design decisions depending on the context.
1

The VooDoo you want is composition. You can solve that using Visitor dessign pattern. But ther is some penalty when you are using it. Or you can create onther interface that will be used to invoke that a3 method.

The case varry. The reason of question might be that your architecture is not consist and you trying to do something strange or your class perform to many things.

1 Comment

how would that additional interface look like, if the current API needs to be unchanged? What do you mean by "not consist". ImplB wants to trigger a state change on ImplA because of an event which is handled by ImplB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.