Currently I have a method that acts as a factory based on a given String. For example:
public Animal createAnimal(String action)
{
if (action.equals(\"M
If you don't have to use Strings, you could use an enum type for the actions, and define an abstract factory method.
...
public enum Action {
MEOW {
@Override
public Animal getAnimal() {
return new Cat();
}
},
WOOF {
@Override
public Animal getAnimal() {
return new Dog();
}
};
public abstract Animal getAnimal();
}
Then you can do things like:
...
Action action = Action.MEOW;
Animal animal = action.getAnimal();
...
It's kind of funky, but it works. This way the compiler will whine if you don't define getAnimal() for every action, and you can't pass in an action that doesn't exist.