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-02 10:36

>>6
Another alternative was proposed on progrider;

Instead of using a list for ship parts, the space ship could have a list for every type of ship parts. This eliminates the need for instanceOf, visitor, or ugly hacks. This probably is preferable when most operation depends on the type of ship part, instead of working with the ship parts as a group.

import java.util.LinkedList;
import java.util.List;

public class SpaceShip
{

    public static class ShipEngine
    {
        public int power = 0;

        public void setEnginePower(int newPower)
        {
            System.err.println("ENGINE POWER MODIFIED : " + power + " TO " + newPower);
            power = newPower;
        }

    }

    public static class ShipWeapon
    {
        public int damage = 5;

        public void setWeaponDamage(int newDamage)
        {
            System.err.println("WEAPON DAMAGE MODIFIED : " + damage + " TO " + newDamage);
            damage = newDamage;
        }

    }

    // The methods and fields of the SpaceShip class

    private final List<ShipEngine> engines = new LinkedList<>();
    private final List<ShipWeapon> weapons = new LinkedList<>();

    public void setEnginesPower(final int powerLevel)
    {
        for (ShipEngine pickedPart : engines)
            pickedPart.setEnginePower(powerLevel);
    }

    public void setWeaponsDamage(final int damage)
    {
        for (ShipWeapon pickedPart : weapons)
            pickedPart.setWeaponDamage(damage);
    }

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

        // two engines and a weapon
        myShip.engines.add(new ShipEngine());
        myShip.engines.add(new ShipEngine());
        myShip.weapons.add(new ShipWeapon());

        myShip.setEnginesPower(100); // power on the engines!
        myShip.setWeaponsDamage(2000); // power the weapons !
    }
}


>>7
I'll check it out

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