Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Visitor vs. instanceof

Name: Anonymous 2013-12-01 16:19

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 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);
    }

}

Name: Anonymous 2013-12-01 18:41

I think that using instanceof is more straightforward.

import java.util.LinkedList;

public class SpaceShip
{
  public static abstract class ShipPart
  {
    public int hull = 10;
  }

  public static class ShipEngine extends ShipPart
  {
    public int power = 0;
  }

  public static class ShipWeapon extends ShipPart
  {
    public int damage = 5;
    public int cooldown = 2;
  }

  private LinkedList<ShipPart> parts = new LinkedList<ShipPart>();

  public void addPart(ShipPart part) {
    parts.add(part);
  }

  public void setEnginesPower(int powerLevel) {
    for(ShipPart pickedPart : parts)
      if(pickedPart instanceof ShipEngine) {
        ((ShipEngine)pickedPart).power = powerLevel;
        System.err.println("POWER LEVEL: " + powerLevel);
      }  
      else
        System.err.println("NOT AN ENGINE!!!");
  }

  public static void main(String[] args) {
    SpaceShip myShip = new SpaceShip();

    myShip.addPart(new ShipEngine());
    myShip.addPart(new ShipEngine());
    myShip.addPart(new ShipWeapon());

    myShip.setEnginesPower(100);
  }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List