jeudi 14 janvier 2016

Populating a zoo object with animal objects using enum in Java

Being used to old-school procedural C programming I am now learning Java and has come to the perhaps obvious insight that the hard bit is design and not syntax.

I have spent hours scrapping idea after idea of how to populate my zoo:

I have a class Zoo and and an abstract class Animal. There are several non-abstract subclasses of Animal called Lion, Giraffe, Zebra, Penguin, and so on. A Zoo object will contain exactly one instance of each subclass of Animal, and each such instance contains a reference to the unique Zoo instance it belongs to.

I would like to iterate over the animals at the zoo in a specific order (as you walk along a footpath say) as well as look up animals at the zoo in a dictionary. More precisely, at some point I am going to parse a text file containing animal names (e.g. for the string "LION" I want to get the unique Lion instance). There is a one-to-one mapping of strings to animals. I have settled on using LinkedHashMap<String, Animal>.

I want to write manageable code that enables me to easily add more animals in future. My best approach so far is as follows.

In the Zoo class I define an enum that reflects the ordering of the animals that I want, and its elements correspond exactly to the strings I will parse in the text file.

private enum Species { LION, GIRAFFE, ZEBRA, PENGUIN };

In Zoo I also have a method that creates an animal object:

private Animal makeAnimal(Species species)
{
    switch (species)
    {
        case LION:
            // create a Lion object;
            break;
        case GIRAFFE:
            // ...
    }
    // return the Animal object created above;
}

As part of the constructor of Zoo I iterate over the enum and insert elements into the LinkedHashMap called animals:

for (Species species : Species.values())
    animals.put(species.name(), makeAnimal(species));

To add a new animal I have to

  1. add a subclass to Animal,
  2. stick a new element into the enum,
  3. add a case to the switch statement in the makeAnimal(Species species) method.

Is this a sound and sane approach? Now after taking the time to write this question down I am actually rather happy with my approach ;), but perhaps I am missing an obvious design pattern and my solution will back-fire at some point. I have the feeling that there is an undesirable separation between the name "LION" and its class Lion that is not ideal.

Aucun commentaire:

Enregistrer un commentaire