Hello guys, How to Eliminate ifs and elses using polymorphism, so that the code is more compact and easy to understand.

public void update( ){
        // is its dead it does nothing
        if( dead ) return;

        // see what movement it is doing
        int dy = rising? -6: 6;
        if( walking && rising )
            move( (dir==LEFT? -6: 6), dy);
        else if( walking )
            move( (dir==LEFT? -4: 4), dy);
        else
            move( 0, dy );

        if( shooting )
            fire();

        // see the bouding rectangle to check for colisions
        Rectangle ra = getBounds(); 


        // if it is not draggin, see if it touches any draggable
        if( !isDragging() ){
            for( WorldElement t : getWorld().getDraggables() ){             
                Rectangle rt = t.getBounds();
                if( rt.intersects( ra ) ){  



                    /* ALL THESE IFS MUST BE REMOVED */ 
                    /* ALL THESE IFS MUST BE REMOVED */ 
                    /* ALL THESE IFS MUST BE REMOVED */ 
                    if( t instanceof Fuel ) {
                        Fuel f = (Fuel)t;
                        draggedFuel = f;
                        f.pickUp();
                    }
                    else if( t instanceof Treasure ){
                        Treasure tre = (Treasure) t;
                        draggedTreasure = tre;
                        tre.pickUp();
                    }
                    else if( t instanceof Spaceship ){
                        Spaceship s = (Spaceship)t;
                        draggedShip = s;
                        s.pickUp();
                    }
                    int dtx = (ra.x - rt.x) + (ra.width - rt.width)/2;
                    int dty = (ra.y - rt.y) + (ra.height - rt.height);
                    t.move(dtx, dty );
                    break;
                }
            }
        }

        // if it is dragging then check if its over the drop area
        if( isDragging() ){
            Rectangle drop = getWorld().getMainSpaceship().getDropArea();

            /* ALL THESE IFS MUST BE REMOVED */ 
            /* ALL THESE IFS MUST BE REMOVED */ 
            /* ALL THESE IFS MUST BE REMOVED */ 
            if( draggedFuel != null ){
                Rectangle rt = draggedFuel.getBounds(); 
                if( drop.intersects( rt ) ){
                    draggedFuel.release();
                    int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2;
                    draggedFuel.move(dtx, 0 );
                    draggedFuel = null;             
                }
            }
            if( draggedTreasure != null ){
                Rectangle rt = draggedTreasure.getBounds(); 
                if( drop.intersects( rt ) ){
                    draggedTreasure.release();
                    int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2;
                    draggedTreasure.move(dtx, 0 );
                    draggedTreasure = null;             
                }
            }
            if( draggedShip != null ){
                Rectangle rt = draggedShip.getBounds(); 
                if( drop.intersects( rt ) ){
                    draggedShip.release();
                    int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2;
                    draggedShip.move(dtx, 0 );
                    draggedShip = null;             
                }
            }           
        }       

        // if the ship is full then test if it hits the ship to win the level
        if( getWorld().getFuelPercentage() == 100 ){
            Spaceship n = getWorld().getMainSpaceship();
            if( n.getBounds().intersects( getBounds() ) ){
                getWorld().completed();
                setPosition( new Point(-100,-100) );
            }
        }
        setJetPacOn( true );
    }


     /*** move the astronaut
     * @param. The x distance to move
     * @param. The y distance to move
     */
    public void move(int dx, int dy ) {
        if( getImage().getPosicao().y + dy < 0 )
            dy = -getImage().getPosicao().y;

        if( getImage().getPosicao().x < 0 )
            dx += getWorld().getWidth();
        else if( getImage().getPosicao().x > getWorld().getWidth() )
            dx -= getWorld().getWidth();

        super.move( dx, dy );   

        // also move the dragged
        if( isDragging() ){



            /*

            /* ALL THESE IFS MUST BE REMOVED */ 
            /* ALL THESE IFS MUST BE REMOVED */ 
            /* ALL THESE IFS MUST BE REMOVED */ 
            if( draggedFuel != null )
                draggedFuel.move( dx, dy );
            else if( draggedTreasure != null )
                draggedTreasure.move( dx, dy );
            else if( draggedShip != null )
                draggedShip.move( dx, dy );
            }
        }
    //}

    /** 
     * drops what it is dragging
     */
    public void drop(){
        if( !isDragging() )
            return;


        /* ALL THESE IFS MUST BE REMOVED */ 
        /* ALL THESE IFS MUST BE REMOVED */ 
        /* ALL THESE IFS MUST BE REMOVED */ 
        if( draggedFuel != null ){
            draggedFuel.release();
            draggedFuel = null;
        }
        else if( draggedTreasure != null ){
            draggedTreasure.release();
            draggedTreasure = null;
        }
        else if( draggedShip != null ){
            draggedShip.release();
            draggedShip = null;
        }

    }

Recommended Answers

All 2 Replies

using polymorphism usually doesn't replace the if's/else statements.
just the code within the if/else blocks would be different.

If you have have

if (instance of A) do A's version of something...
if (instance of B) do B's version of something...

then that's usually a sign that A and B should be subclasses of some common superclass, with something as a method that is overridden in A and B

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.