How can Polymorphism replace an if-else statement inside of a loop?

14,094

Solution 1

Polymorphism usually replaces switch statements when each case corresponds to a different type. So instead of having:

public class Operator
{
    string operation;

    public int Execute(int x, int y)
    {
         switch(operation)
         {
             case "Add":
                 return x + y;
             case "Subtract":
                 return x - y;
             case "Multiply":
                 return x * y;
             case "Divide":
                 return x / y;
             default:
                 throw new InvalidOperationException("Unsupported operation");
         }
    }
}

you'd have:

public abstract class Operator
{
    public abstract int Execute(int x, int y);
}

public class Add : Operator
{
    public override int Execute(int x, int y)
    {
        return x + y;
    }
}

// etc

However, for the comparison type of decision you provided, polymorphism really doesn't help.

Solution 2

Polymorphism is not really applicable in the example you provided.

See this SO answer.

Solution 3

In Smalltalk, "if" is actually a polymorphic method in Boolean. In the following example:

[ x>y ] whileTrue:  
  [   
    ( x<z ) ifTrue: [ x:=z ]        
  ]

The ifTrue:aBlock message is implemented in True as "execute this block" and in False as "ignore this block", so depending on what the (x<z) evaluates to, either implementation will be called.

So in Smalltalk polymorphism replaces every if-else construct by default :)

Solution 4

Polymorphism can only replace if tests when the if test is basically dispatchi g to a variety of methods depending on the "type" of an object. Eg if object is type X invoke foo if it's a Y invoke bar and so. In this contrived example one would define an interface DoSonething with a method bad(). Both X and Y would implement Baz and have their respective baz() invoke foo() for X and bar() for Y. This simply calling baz() would eliminate the need for a if test.

Solution 5

One pattern is to have objects which represent the result of the test, and objects which represent the block to execute. The result objects have overridden selection functions, so if Bool had a choose(T positive, T negative) then Bool.TRUE would return the positive argument and Bool.FALSE would return the negative. Naive implementations of smalltalk-family languages work like that.

To encode your while loop in that form, there needs to call the choose method on the result of comparing x and y to determine whether to call the block for the inside of the while loop, and that block also uses compare and choose to set the value of x. A more literal translation would be to choose either a block which sets x to z or one which does nothing; instead it just uses choose to set x back to the same value.

Obviously it's overkill and inefficient for this simple case.

public class WantonPolymorphism {

    static class Int32 {
        final int value;
        Int32 ( final int value ) { this.value = value; }

        Compare compare ( Int32 other ) {
            // Java runs out of turtles at this point unless you use
            // an enum for every value
            if ( this.value < other.value ) return Compare.LESS;
            if ( this.value > other.value ) return Compare.GREATER;
            return Compare.EQUAL;
        }
    }

    enum Compare {
        LESS {
            <T> T choose (T less, T equal, T greater) { return less; }
        },
        EQUAL {
            <T> T choose (T less, T equal, T greater) { return equal; }
        },
        GREATER {
            <T> T choose (T less, T equal, T greater) { return greater; }
        };

        abstract <T> T choose (T less, T equal, T greater) ;
    }

    interface Block { Block execute () ; }


    /**
     * Main entry point for application.
     * @param args The command line arguments.
     */
    public static void main (String...args) {
        Block block =  new Block() {
            Int32 x = new Int32(4);
            Int32 y = new Int32(3);
            Int32 z = new Int32(2);

            public Block execute () {
                System.out.printf("x = %d, y = %d, z = %d\n", x.value, y.value, z.value);

                return x.compare(y).choose(done, done, new Block () {
                    public Block execute () {
                        x = x.compare(z).choose(x,x,z);

                        return x.compare(y).choose(done, done, this);
                    }
                });
            }

            Block done = new Block () {
                public Block execute () {
                    System.out.printf("x = %d, y = %d, z = %d\n", x.value, y.value, z.value);
                    System.exit(0);
                    return this;
                }
            };
        };

        for(;;) 
            block = block.execute();
    }
}
Share:
14,094
WolfmanDragon
Author by

WolfmanDragon

Java, Web and PostgreSQL

Updated on June 05, 2022

Comments

  • WolfmanDragon
    WolfmanDragon almost 2 years

    How can polymorphism replace an if-else statement or Switch inside of a loop? In particular can it always replace an if-else? Most of the if-thens I use inside of loops are arithmetic comparisons. This question is spawned from this question.

    int x;
    int y;
    int z;
    
    while (x > y)
    {
         if (x < z)
         {
             x = z;
         }
    }
    

    How would this work with polymorphism?
    NOTE: I wrote this in Java but am interested in this for any OOL.