How to avoid large if-statements and instanceof

前端 未结 9 2332
盖世英雄少女心
盖世英雄少女心 2020-11-29 05:11

Animal

public abstract class Animal {
 String name;

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

}

Lion<

相关标签:
9条回答
  • 2020-11-29 05:50

    Here you have a List of animals. Usually when you have a list of Objects, all these objects must be able to do the same thing without being casted.

    So the best two solutions are :

    • Having a common method for the two concrete classes (so defined as abstract in Animal)
    • Separate Lion from Deer from the start, and have two different lists.
    0 讨论(0)
  • 2020-11-29 05:52

    Yes provide a method called action() in abstract class , implement it in both of the child class, one will roar other will runaway

    0 讨论(0)
  • 2020-11-29 05:52

    Consider adding an interface for the action (Roar, Run away, etc) which is set on the animal in the constructor. Then have an abstract method such as act() on the Animal class which gets called similar to what Adeel has.

    This will let you swap in actions to act out via a field at any time.

    0 讨论(0)
  • 2020-11-29 05:54

    If your method is not polymorphic you can't do without the cast. To make it polymorphic, declare a method in the base class and override it in the descendant classes.

    0 讨论(0)
  • 2020-11-29 06:07

    The simplest approach is to have the super class implement a default behaviour.

    public enum AnimalBehaviour { 
         Deer { public void runAway() { System.out.println("Running..."); } },
         Lion { public void roar() { System.out.println("Roar"); } }
         public void runAway() { } 
         public void roar() { }
     } 
    
     public class Animal {
         private final String name;
         private final AnimalBehaviour behaviour;
         public Animal(String name, AnimalBehaviour behaviour) {
             this.name = name;
             this.behaviour = behaviour;
         }
         public void runAway() { behaviour.runAway(); } 
         public void roar() { behaviour.roar(); }
      }
    
     public class TestAnimals { 
       public static void main(String... args) { 
         Animal[] animals = { 
           new Animal("Geo", AnimalBehaviour.Lion), 
           new Animal("Bambi", AnimalBehaviour.Deer), 
           new Animal("D2", AnimalBehaviour.Deer) 
         }; 
    
         for (Animal a : animals) {
           a.roar(); 
           a.runAway(); 
         } 
       }
     }
    
    0 讨论(0)
  • 2020-11-29 06:09

    Pattern matching support in the language eliminates the need for the ugly visitor pattern.

    See this Scala code for example:

    abstract class Animal(name: String)
    
    class Lion(name: String) extends Animal(name) {
      def roar() {
        println("Roar!")
      }
    }
    
    class Deer(name: String) extends Animal(name) {
      def runAway() {
        println("Running!")
      }
    }
    
    object TestAnimals {
      def main(args: Array[String]) {
        val animals = List(new Lion("Geo"), new Deer("D1"), new Deer("D2"))
        for(animal <- animals) animal match {
          case l: Lion => l.roar()
          case d: Deer => d.runAway()
          case _       => ()
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题