I'm having this specific issue I couldn't sort out with other questions. I'm trying to implement a Visitor for a game, the visitor is an Attack
class and it has to search in a matrix for cells that may contain Characters
, then if the character is an enemy and not a friend, damage it.
What I'm having trouble is not using an InstanceOf
to visit the character, as it breaks the Open-Closed Principle. Here's my code:
Visitor interface
public interface Visitor {
public void visit(GroundCell c);
public void visit(MountainCell c);
public void visit(BuildingCell c);
public void visit(WaterCell c);
public void visit(Foe f);
public void visit(Friend f);}
Attack abstract class
public abstract class Attack implements Visitor {
}
Attack concrete class
public class TankAttack extends Attack{
...
@Override
public void visit(GroundCell c) {
//here, i'd like to call c.getCharacter.accept(this)
}
But I get an error that says I should first implement Visit(Character c), when I need it only for its subclasses.
What should I do in the TankAttack class to visit the Friend or Foe subclasses of Character and not break the design using InstanceOf?
Edit for clarification: Friend and Foe are subclasses of Character.
Aucun commentaire:
Enregistrer un commentaire