lundi 23 novembre 2015

Decide upon method usage at runtime. Polymorphism

Well, this is kind of embarrassing, but I've forgotten how to do the following in plain old Java:

abstract class Animal {
   protected String name;
   public Animal(String name) {
       this.name = name;
   }
}

class Flea extends Animal {
    private double jumpHeight;

    public Flea(String name, double jumpHeight) {
        super(name);
        this.jumpHeight = jumpHeight;
    }

    public double jump() {
       return jumpHeight();
    }
}

class Giraffe extends Animal {
    private int strideLength;

    public Giraffe(int strideLength) {
        super("Berta");
        this.strideLength = strideLength;
    }

    public int stride() { return strideLength; }
}

class Gorilla extends Animal {
    private String call;

    public Gorilla(String call) {
        super("Boris");
        this.call = call;
    }

    public String call() { return "Gorilla says " + call; }
}

Now I would like to decide the appropriate method at runtime, without having to add all the methods to each Animal (no abstract methods) and without meaningless placeholders (like imagine a Flea has no call).

I would like to do this without casting. So no:

if(Animal instanceof Gorilla) ((Gorilla) animal).call();

I could come up with a solution incorporating interfaces like jumpable and could use that, but I'm sure there was a certain pattern that was exactly for this kind of task.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire