Name: Anonymous 2013-12-01 20:27
Hello /prog/,
I'd like to know what do you think of this. When you must add an operation that only affects some elements of a list based on their type, would you use a visitor, or instanceof?
Here's an example where I want to power all the engines of a space ship. Would you make different lists for every type of parts, use
I'd like to know what do you think of this. When you must add an operation that only affects some elements of a list based on their type, would you use a visitor, or instanceof?
Here's an example where I want to power all the engines of a space ship. Would you make different lists for every type of parts, use
instanceof ShipEngine
, or use a visitor?import java.util.LinkedList;
import java.util.List;
public class SpaceShip
{
// The ship parts
public static interface ShipPart
{
void accept(ShipPartVisitor visitor);
}
public static class ShipEngine implements ShipPart
{
public int power = 0;
@Override
public void accept(ShipPartVisitor visitor)
{
visitor.visit(this);
}
}
public static class ShipWeapon implements ShipPart
{
public int damage = 5, cooldown = 2;
@Override
public void accept(ShipPartVisitor visitor)
{
visitor.visit(this);
}
}
// The interface for visitors
public static interface ShipPartVisitor
{
void visit(ShipEngine shipEngine);
void visit(ShipWeapon shipWeapon);
}
// The methods and fields of the SpaceShip class
private final List<ShipPart> parts = new LinkedList<>();
public void addPart(ShipPart part)
{
parts.add(part);
}
public void setEnginesPower(final int powerLevel)
{
ShipPartVisitor visitor = new ShipPartVisitor()
{
@Override
public void visit(ShipWeapon shipWeapon)
{
// we're not playing with the weapons
}
@Override
public void visit(ShipEngine shipEngine)
{
shipEngine.power = powerLevel;
System.err.println("POWER LEVEL : " + powerLevel);
}
};
for (ShipPart pickedPart : parts)
pickedPart.accept(visitor);
}
public static void main(String[] args)
{
SpaceShip myShip = new SpaceShip();
// two engines and a weapon
myShip.addPart(new ShipEngine());
myShip.addPart(new ShipEngine());
myShip.addPart(new ShipWeapon());
// power on the engines!
myShip.setEnginesPower(100);
}
}