0

For example, I have a class called animals. I don't think I need to subclass this into birds, mammals, etc, because I only want to have one class that contains methods and ui for all animals, e.g. show animal being born, show animal growing, show animal being butchered, show animal being eaten (aside: yes, this is just an example but you get the picture). However, I will only want to load all the birds and action them then return to the main menu, then navigate to the mammals and show them, and so on. In my real app each "animal" will have 3x int of information only. There is no video but it will have continuous audio and animation.

Since I only use 1/6 of the animal data each time, ie 10-15 animals, maybe 60-100 animals for the entire app what am I best off doing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John Ashmore
  • 1,015
  • 1
  • 10
  • 25
  • 1
    Why don't you want to subclass? It seems to me that an interface `Animal` with all the functions you need like `drawEating()` is the thing you want. Then each subclass can store its info in private class variables. – Ben Ruijl Jul 26 '12 at 15:35
  • Oh, ok then. If I do that then I will have a single UI activity. How will I pass onto that activity which class of animals to use? – John Ashmore Jul 26 '12 at 15:52

2 Answers2

0

Comment turned into answer

I would create an interface called Animal which contains all the functions.

public interface Animal {
   public void drawEating();
   public void drawDeath();
}

Then create classes implementing this interface:

public class Bird implements Animal {
   public void drawEating() {
       // TODO: fill in
   }
   public void drawDeath() {
      // TODO: fill in
   }
}

To pass the Animal you want to draw to a new Activity you can pass it in a bundle like described here, or create a function in your new activity called registerAnimal and call that from your first activity.

public class AnimalActivity extends Activity {
   Animal animal = null;


 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 }

 public void registerAnimal(Animal a) {
      animal = a;
 }

 public void someDrawFunction() {
      if (animal != null)
          animal.drawEating();
  }

}

Using this scheme you only have one activity for all animals, thus saving lots of repetetive code.

Community
  • 1
  • 1
Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
  • I think you mean I should add the methods all to animal once and not to the subclasses each time. Either way I'll give you the tick. Thanks. – John Ashmore Jul 27 '12 at 06:44
0

If you like to use some basic fields of animal class i.e name, color, etc you should create an abstract class animal instead an interface.

Roger Garzon Nieto
  • 6,554
  • 2
  • 28
  • 24