I have following class diagram (visitor pattern implementation):
Expected result:
1) WiredVisitor should visit only Router and WiredNetworkCard
2) WirelessVisitor should visit only Router and WirelessNetworkCard
So, my question is: How should I change design (or code) for achieving my expected result?
P.S. My current solution is to add following piece of code to each visit(card:INetworkCard) method in both visitors:
// in WiredVisitor
if (card.getClass.equals(WiredNetworkCard.class)){
// do logic of visit method
}
// in WirelessVisitor
if (card.getClass.equals(WirelessNetworkCard.class)){
// do logic of visit method
}

In the spirit of the acyclic visitor pattern, separate your visitors into subclass specific ones. Note that you’ll still need the type check, but it’s contained to the type being visited:
Here are the visitor interfaces:
The concrete visitors will look like this:
And the visited objects:
In those type checks, you can also throw an error if the type is not the expected one, depending on what you’d like to happen in that case.