9

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.

2
  • programmers like to populate the zoo and solve abstract riddles but such kind of problems almost never appear in real life Jan 19, 2016 at 8:27
  • @AdamSkywalker - The "populating a zoo" scenario is of course a translation from my actual programming issue with completely different objects. I thought animals and zoo made a more straightforward presentation. You are right though, there are details lost in the translation that do appear in my actual programming situation. However, I think the answers to my question have given me lots to think about and provided good and useful help to take home. But yes, it is annoying when all those beautiful ideas (in theory) never really suit your real-world needs without ugly modifications.
    – DustByte
    Jan 20, 2016 at 10:42

5 Answers 5

9

Requirements

Reading your question I found the following requirements of Zoo:

  1. Lookup animals by their species name
  2. Define order of species
  3. Easily add animals in the future

1. Lookup animals by their species name

As you mentioned there is an undesirable separation between the string "LION" and the class Lion. In Effective Java, Item 50, the following is stated about strings:

Strings are poor substitutes for other value types. When a piece of data comes into a program from a file, from the network, or from keyboard input, it is often in string form. There is a natural tendency to leave it that way, but this tendency is justified only if the data really is textual in nature. If it’s numeric, it should be translated into the appropriate numeric type, such as int, float, or BigInteger. If it’s the answer to a yes-or-no question, it should be translated into a boolean. More generally, if there’s an appropriate value type, whether primitive or object reference, you should use it; if there isn’t, you should write one. While this advice may seem obvious, it is often violated.

Therefore, instead of looking up animals by their species name you should lookup animals by their species instance.

2. Define order of species

To define the ordering of species you need to use a collection with a predictable iteration order, such as the mentioned LinkedHashMap.

3. Easily add animals in the future

Adding animals currently consists of the three steps you mentioned. A side effect of the usage of an enum is that only a person with access to the source code has the capability to add new species (as the Species enum has to be extended).

Now consider, Species.LION, this is the species of the Lion class. Note that this relationship is semantically the same as the relation between a class and its instantiation. Therefore, a much more elegant solution would be to use Lion.class as the species of Lion. This also reduces the number of steps for adding an animal as you get the species for free.

Analysis of other parts of the code

Zoo

In your proposal, the zoo has the responsibility to create animals. The consequence is that every zoo ever created needs to use all defined animals (because of the Species enum) and use the specified ordering, there would be no variation among zoo's. It is better to decouple the creation of animals from the zoo in order to allow more flexibility of both the animals and the zoo.

Animal

Because of their flexibility, interfaces should be preferred over abstract classes. As explained in Effective Java, Item 18:

Item 18. Prefer interfaces to abstract classes

The Java programming language provides two mechanisms for defining a type that permits multiple implementations: interfaces and abstract classes. The most obvious difference between the two mechanisms is that abstract classes are permitted to contain implementations for some methods while interfaces are not. A more important difference is that to implement the type defined by an abstract class, a class must be a subclass of the abstract class. Any class that defines all of the required methods and obeys the general contract is permitted to implement an interface, regardless of where the class resides in the class hierarchy. Because Java permits only single inheritance, this restriction on abstract classes severely constrains their use as type definitions.

Circular references

In your question you mention that an animal should also have a reference to the zoo in which it resides. This introduces a circular reference which should be avoided as much as possible.

One of many disadvantages of circular references is:

Circular class references create high coupling; both classes must be recompiled every time either of them is changed.

Example implementation

public interface Animal {}

public class Zoo {
  private final SetMultimap<Class<? extends Animal>, Animal> animals;

  public Zoo() {
    animals = LinkedHashMultimap.create();
  }

  public void addAnimal(Animal animal) {
    animals.put(animal.getClass(), animal);
  }

  @SuppressWarnings("unchecked") // the cast is safe
  public <T extends Animal> Set<T> getAnimals(Class<T> species) {
    return (Set<T>) animals.get(species);
  }
}

Usage

static class Lion implements Animal {}

static class Zebra implements Animal {}

final Zoo zoo = new Zoo();
zoo.addAnimal(new Lion());
zoo.addAnimal(new Zebra());
zoo.addAnimal(new Lion());

zoo.getAnimals(Lion.class); // returns two lion instances

zoo.getSpeciesOrdering(); // returns [Lion.class, Zebra.class]

Discussion

The implementation above has support for multiple animal instances per species, as that seems to make more sense for a zoo. If only one animal is needed consider using Guava's ClassToInstanceMap instead of the SetMultimap.

The creation of the animals was not considered as part of the design problem. If more complex animals need to be constructed, consider using the builder pattern.

Adding an animal is now as simple as creating a new class that implements the Animal interface and adding it to the zoo.

2
  • Been reading your answer a few times, and I find it particularly appealing to separate the creation of the animals from the Zoo class. As to "Strings are poor substitutes for other value types", in my case the animal names will appear in a text file that humans can/are supposed to read, so I am sort of "stuck" with names in a readable format, i.e. strings. It does however leave me pondering if perhaps I should reconsider the data formal I use completely, but that is outside the scope of my question.
    – DustByte
    Jan 18, 2016 at 10:23
  • The moment a string enters you program you should interpret it, i.e. convert it to the correct representation. For example, you could create a ZooParser class that converts animal names to Animal instances. The added benefit is that if somebody supplies a wrong animal name the error occurs right at the source of the problem, if the parsing would happen at a later stage the error might occur at an completely unexpected place.
    – rinde
    Jan 18, 2016 at 10:32
5

While maybe a minor thing, one should take care to avoid unnecessary circular references/dependencies. I don't want to invoke the dogmatic point of view that all forms of circular dependencies are bad, but depending on how you want to use your program, you might be better of not having an animal depend on a zoo and a zoo depend on multiple animals at the same time.

In your example, the class Zoo and Animal are perhaps more tightly coupled than they have to be. If you want to relocate an animal to another zoo, you have to update both the zoo instance and the animal instance. If you want to maintain bidirectional navigability, you could use e.g. a bidirectional map in Zoo, as Zoo would have the responsibility of aggregating animals (which is also what a zoo does in the real world, come to think of it) but Zoo would then also be the sole place where this relationship is maintained. In my opinion, this is cleaner.

Another discussion on the point of circular references can be found here: https://softwareengineering.stackexchange.com/questions/11856/whats-wrong-with-circular-references

1
  • Right, I've had this bad taste in my mouth after creating all these circular references. This is something I need to address.
    – DustByte
    Jan 18, 2016 at 10:24
4

Yes, your approach seems correct.

However, if you have only one animal by species, maybe you can directly instantiate it in a static field without need of a specific method.

You can also add a factory class like this:

interface AnimalFactory<T extends Animal> {
    T make();
}

and implements a factory for each kind of animal:

class LionFactory implements AnimalFactory<Lion> {
     public Lion make() {
         return new Lion(); 
     }
}

And you can also add this factory directly in your enum:

enum Species { 
    LION ( new LionFactory() ), 
    // As AnimalFactory is a functional interface, you can use lambda expressions
    GIRAFFE ( () -> new Giraffe() );

    private AnimalFactory _factory;

    Species( AnimalFactory factory ) {
        this._factory = factory;
    }

    AnimalFactory getFactory() {
        return _factory;
    }
}

and then, if you want a giraffe, you have just to call:

Animal giraffe = Species.GIRAFFE.getFactory().make();
3

Overall your design looks good, I'll address several points you've raised:

  1. I suggests an interface Animal that other concrete animal class will inherit from - so you could access all animals the same way.
  2. Determine the order - Here is an option:

    List<Animal> animals = new ArrayList<Animal>(); // List
    animals.Add(new Lion()); // Order 0
    animals.Add(new Giraffe()); // Order 1
    animals.Add(new Zebra()); // Order 2
    

    You can replace the new ConcreteAnimal() with a method that create the instance. Something like:

    Animal CreateAnimal(AnimalType animalType);
    

    List (and ArrayList) is a sequential list. So, insertion and retrieval order is guaranteed the same. Also, you can add / remove animal type very easily.

2

The design pattern that you have used very much resembles to Factory Design Pattern. which comes under the category of Creational Design Pattern. These kind of design patterns deal with creations of objects.

The method which creates the objects for animals makeAnimal(Species species) can be put in a separate class which can essentially be called something in the order of "AnimalFactory" or something like that.

Other than that I guess your design is sound.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.