Skip to main content
1 of 5
Doc Brown
  • 220.3k
  • 35
  • 410
  • 623

I would suggest to implement ElementA.accept in terms of the base class Visitor, and ElementB.accept in terms of Visitor2.

public ElementA {
    public void accept(Visitor v) {
        //...
    }
}

public ElementB {
    public void accept(Visitor2 v) {
        //...
    }
}

That way, you avoid the code duplication which leads to the extendability problem mentioned in your question.

Doc Brown
  • 220.3k
  • 35
  • 410
  • 623