I have a collection of Animal objects.
My core code wants to treat all of these as Animals, all the same. Each Animal needs to be processed in some way. The nature of the processing depends on the sub-type of the Animal (bird, mammal, etc).
My code currently looks as follows.
public interface Animal {
public String getTaxonomyClass();
}
public abstract class Bird implements Animal {
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public abstract float getWingspan();
}
public abstract class Mammal implements Animal {
#Override
public String getTaxonomyClass() {
return "mammalia";
}
// Specific to mammals
public abstract int getToothCount();
}
public interface AnimalProcessor {
public String getSupportedTaxonomyClass();
public void process(Animal a);
}
public class MammalProcessor implements AnimalProcessor {
#Override
public String getSupportedTaxonomyClass() {
return "mammalia";
}
#Override
public void process(Animal a) {
System.out.println("Tooth count is " + ((Mammal)a).getToothCount());
}
}
public class BirdProcessor implements AnimalProcessor {
#Override
public String getSupportedTaxonomyClass() {
return "aves";
}
#Override
public void process(Animal a) {
System.out.print("Wingspan is " + ((Bird)a).getWingspan());
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ZooKeeper {
Map<String, AnimalProcessor> registry = new HashMap<String, AnimalProcessor>();
public void registerProcessor(AnimalProcessor ap)
{
registry.put(ap.getSupportedTaxonomyClass(), ap);
}
public void processNewAnimals(List<Animal> newcomers)
{
for(Animal critter : newcomers)
{
String taxonomy = critter.getTaxonomyClass();
if(registry.containsKey(taxonomy))
{
// if I can process the animal, I will
AnimalProcessor ap = registry.get(taxonomy);
ap.process(critter);
}
}
}
}
import java.util.LinkedList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ZooKeeper keeper = new ZooKeeper();
keeper.registerProcessor(new MammalProcessor());
keeper.registerProcessor(new BirdProcessor());
List<Animal> animals = new LinkedList<Animal>();
animals.add(new Mammal() { // badger
#Override
public int getToothCount() {
return 40;
} }
);
animals.add(new Bird() { // condor
#Override
public float getWingspan() {
return 2.9f;
} }
);
keeper.processNewAnimals(animals);
}
}
Generally this is easy to understand and works nicely! I can add plug-in new processors and animal types at my leisure without changing the ZooKeeper class or any of the interfaces. You can imagine a more advanced main class, loading the Animals from a database, and processing them all in turn.
However, I worry about the downcasts inside the AnimalProcessor subclasses! This strikes me as something which should not be there, and may be a violation of OO principles. After all, at the moment I can pass a Bird to a MammalProcessor's process() method, and there will be a ClassCastException.
Can anyone suggest a design pattern to solve this? I looked at the Visitor pattern, but couldn't quite figure out how to apply it in this case! The key is to make the core code (ZooKeeper) treat all animals the same, and make it so that support for new Animals can be added trivially. Thanks!
I would suggest the following:
public interface Animal {
public AnimalProcessor<? extends Animal> getProcessor();
}
so each animal will return it's matching processor.
public interface AnimalProcessor<T extends Animal> {
public void process(T a);
}
so the processors will be typed with their matching type it's should process.
so the implantation will be like this:
public abstract class Bird implements Animal {
private BirdProcessor processor = new BirdProcessor();
public abstract float getWingspan();
#Override
public AnimalProcessor<Bird> getProcessor() {
return processor;
}
}
public class BirdProcessor implements AnimalProcessor<Bird> {
#Override
public void process(Bird b) {
System.out.print("Wingspan is " + b.getWingspan());
}
}
This is where generics work great.
First, you need to make AnimalProcessor generic:
public interface AnimalProcessor <T extends Animal> {
public String getSupportedTaxonomyClass();
public void process(T a);
}
Next, in your specific processors, you specify the generic type - eg for mammals:
public class MammalProcessor implements AnimalProcessor<Mammal> {
public String getSupportedTaxonomyClass() {
return "mammalia";
}
public void process(Mammal a) {
System.out.println("Tooth count is " + a.getToothCount());
}
}
Now, the process method only accepts Mammal objects, no birds here.
I suggest the following :
public interface Animal {
public String getTaxonomyClass();
public void process();
}
Now each animal class implementing Animal should implement its own processing logic.
For example :
public class Bird implements Animal {
public Bird(float wingSpan) {
this.wingSpan = wingSpan;
}
#Override
public String getTaxonomyClass() {
return "aves";
}
#Override
public void process() {
System.out.print("Wingspan is " + wingSpan);
}
// Specific to birds
private float wingspan;
}
Now you can have only one AnimalProcessor which processes as follows :
public void process(Animal a) {
a.process();
}
Make you AnimalProcessor generic;
public interface AnimalProcessor<T extends Animal> {
public String getSupportedTaxonomyClass();
public void process(T a);
}
public class MammalProcessor implements AnimalProcessor<Mammal> {
#Override
public String getSupportedTaxonomyClass() {
return "mammalia";
}
#Override
public void process(Mammal a) {
System.out.println("Tooth count is " + a.getToothCount());
}
}
So you've got a class like this...
public abstract class Bird implements Animal {
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public abstract float getWingspan();
}
All Birds will have a wingspan, even if the wingspan is 0. So, why don't you change the class to something like this...
public class Bird implements Animal {
float wingspan = 0.0f;
public Bird(float wingspan){
this.wingspan = wingspan;
}
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public float getWingspan(){
return wingspan;
}
}
So, to create a new Bird, instead of doing this...
animals.add(new Bird() { // condor
#Override
public float getWingspan() {
return 2.9f;
} }
);
You would just do this...
animals.add(new Bird(2.9f)); // condor
This would seem to make the whole thing a lot simpler and nicer for your purposes. You would do a similar change for your Mammal class too.
Now, for the processing of animals... if all Animals are going to be processed, you could just implement process() in Bird rather than needing a separate BirdProcessor class. To do this, in Animal, declare a method public void process();. Your Bird would implement it like this...
public void process() {
System.out.print("Wingspan is " + getWingspan());
}
and you would change your AnimalProcessor to simply do this (note: no longer an interface)...
public class AnimalProcessor {
public void process(Animal a) {
a.process();
}
}
Your AnimalProcessor class would then be able to handle all Animals.
Alternatively, if you want to leave AnimalProcessor as it is, it would probably be good to change the following though, to avoid the ClassCastException (this code here is for the BirdProcessor)...
public void process(Animal a) {
if (a instanceof Bird){
System.out.print("Wingspan is " + ((Bird)a).getWingspan());
}
}
Is this kinda what you were looking for?
Your problem are methods such as
public abstract int getToothCount();
...aren't defined in Animal. Instead, they are defined in specific subclasses of Animal. This means you can't treat Animals generically since they are fundamentally different.
To overcome this, the one approach would be to create abstract methods for all these in the Animal class.
Bird might respond to getToothCount() with "0".
Since all animals could respond to getWingspan(), getTootCount(), etc, you would not have to perform any type-specific checking. If this isn't good enough, create abstract implementations of "boolean hasWings()", "boolean hasTeeth()" etc etc in Animal.
Now you could say, for some animal a:
if (a.hasWings()) System.out.println("My wingspan is "+a.getWingSpan());
which would work for any animal. Of course, each subclass of Animal would have to implement all the various methods.
Another option is to add non-abstract methods to Animal. These methods would supply default answers. For example, getWingSpan() would return 0, getToothCount() would return 0, etc. Shark would override getToothCount(). Eagle would override getWingSpan()...
Then your subclasses would only have to override (or even know about) methods related directly to them.
Related
So the compiler complains when ever i do a explicit cast. I can prevent this by using a #SuppressWarnings annotation.
At this point i would have this annotation a lot in my code which lets me suspect that there is another way i'm just not aware of.
Lets have a look at this example
class CutePet
{
public void pet()
{
System.out.println( "The cute pet gets some pets" );
}
}
class Cat extends CutePet
{
public void letOutside()
{
System.out.println( "The cat goes outside" );
}
public void letInside()
{
System.out.println( "The cat comes inside" );
}
public void removeTick()
{
System.out.println( "The cat looses all ticks" );
}
}
class Dog extends CutePet
{
public void goForAWalk()
{
System.out.println( "The Dog goes for a walk" );
}
public void tellHimWhatHeIs()
{
System.out.println( "The Dog is a good boy" );
}
}
class caretaker
{
public void takeCare( CutePet pet )
{
if( pet instanceof Cat )
{
pet.pet();
((Cat)pet).letOutside();
((Cat)pet).letInside();
((Cat)pet).removeTick();
}
else if( pet instanceof Dog )
{
pet.pet();
((Dog)pet).goForAWalk();
((Dog)pet).tellHimWhatHeIs();
}
}
}
The Caretaker does not know what kind of Pet he will get in advance and he my has several pets of different kinds.
I tried to give the Cute pet class a getType() method which returns a enum. With this enum i can remove the "instanceof" but the cast is still there.
Am i missing something?
If this were a real world problem, the caretaker would recognize which kind of pet he has based on the pet's appearance. While "instance of" is one way of looking at it, you might want to consider overloading the takeCare method directly with the subtypes as required. For example:
class Caretaker {
public void takeCare(Cat pet) {
pet.pet();
pet.letOutside();
pet.letInside();
pet.removeTick();
}
public void takeCare(Dog pet) {
pet.pet();
pet.goForAWalk();
pet.tellHimWhatHeIs();
}
}
in other words, the caretaker knows what to do (has methods already in place) for the kind of pet he receives.
EDIT
In response to some of the comments, yes, the original example shifts the problem further up. If you have an array or a list of generic pets then you still have to figure out what kinds of pets you have to give them to the caretaker. Conceptually it seems strange that the pet should be able to pet itself, take itself for a walk, etc. (these methods are part of the pet class when it should be the caretaker doing these actions ON the pet).
I've since rewritten the code with a full working example below with a Job class that has a perform method. This method will return the appropriate job based on the type of animal the caretaker has. The caretaker can then perform the job on the pet in question. See below.
Doing things this way avoids instanceof. While it is debatable how good/bad instanceof actually is, where possible it should be the object itself to tell me what it needs, otherwise the whole polymorphism concept can get pretty hairy pretty quick.
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
Caretaker caretaker = new Caretaker();
Arrays.asList(
new Cat("Cat1"),
new Cat("Cat2"),
new Dog("Dog1")
).forEach(caretaker::takeCare);
}
interface CutePet {
String whoAmI();
Job whatINeed();
}
abstract static class NamedCutePet implements CutePet {
private final String name;
public NamedCutePet(String name) {
this.name = name;
}
public String whoAmI() {
return this.name;
}
}
static class Cat extends NamedCutePet {
public Cat(String name) {
super(name);
}
#Override
public Job whatINeed() {
return new CatJob(this);
}
}
static class Dog extends NamedCutePet {
public Dog(String name) {
super(name);
}
#Override
public Job whatINeed() {
return new DogJob(this);
}
}
static class Caretaker {
void takeCare(CutePet pet) {
pet.whatINeed().perform();
}
}
static abstract class BaseJob implements Job {
void pet(CutePet pet) {
System.out.println(String.format("The cute pet %s gets some pets", pet.whoAmI()));
}
}
static class DogJob extends BaseJob {
private final Dog dog;
public DogJob(Dog dog) {
this.dog = dog;
}
#Override
public void perform() {
pet(dog);
takeDogFarAWalk(dog);
tellHimWhatHeIs(dog);
}
private void takeDogFarAWalk(Dog dog) {
System.out.println(String.format("The dog %s goes for a walk", dog.whoAmI()));
}
private void tellHimWhatHeIs(Dog dog) {
System.out.println(String.format("The dog %s is a good boy", dog.whoAmI()));
}
}
static class CatJob extends BaseJob {
private final Cat cat;
public CatJob(Cat cat) {
this.cat = cat;
}
#Override
public void perform() {
pet(cat);
letOutside(cat);
letInside(cat);
removeTick(cat);
}
private void letOutside(Cat cat) {
System.out.println(String.format("The cat %s goes outside", cat.whoAmI()));
}
private void letInside(Cat cat) {
System.out.println(String.format("The cat %s comes inside", cat.whoAmI()));
}
private void removeTick(Cat cat) {
System.out.println(String.format("The cat %s loses all ticks", cat.whoAmI()));
}
}
interface Job {
void perform();
}
}
Let's make it clear: you can't call subclass specific methods without typecasting to subclass type.
Now, let me suggest an alternate way. Define a method takeCare() in the superclass and let the subclasses implement it by calling several specific methods specific to subclasses. Then from CareTaker#takeCare(), call only takeCare() method without typecasting.
Several other alternate approaches can be used to solve the situation.
Here is how you would do it with interfaces and reflection. Note that only the interface methods are called for each pet type. It could also be extended to call other methods.
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class PetProblem {
public static void main(String[] args) {
Caretaker caretaker = new Caretaker();
Dog dog = new Dog();
caretaker.takeCare(dog);
System.out.println("\nNow do it for the cat\n");
Cat cat = new Cat();
caretaker.takeCare(cat);
}
}
interface CuteCat {
void letOutside();
void letInside();
void removeTick();
}
interface CuteDog {
void goForAWalk();
void tellHimWhatHeIs();
}
interface CutePet {
default void pet() {
System.out.println("The cute pet gets some pets");
}
}
class Cat implements CutePet, CuteCat {
public void letOutside() {
System.out.println("The cat goes outside");
}
public void letInside() {
System.out.println("The cat comes inside");
}
public void removeTick() {
System.out.println("The cat looses all ticks");
}
}
class Dog implements CutePet, CuteDog {
public void goForAWalk() {
System.out.println("The Dog goes for a walk");
}
public void tellHimWhatHeIs() {
System.out.println("The Dog is a good boy");
}
}
class Caretaker {
public void takeCare(Object pet) {
Class<?>[] ifss = pet.getClass().getInterfaces();
for (Class<?> ifs : ifss) {
Method[] methods = ifs.getDeclaredMethods();
for (Method m : methods) {
try {
m.invoke(pet);
}
catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
Note however, that using interfaces and having a method so named that it can be used for all pets is easier. Here is an example. Since both dogs and cats need to eat, a common method feedMe() can be implemented for each.
public class AnimalShelter {
public static void main(String[] args) {
Caretaker caretaker = new Caretaker();
Dog dog = new Dog();
Cat cat = new Cat();
caretaker.feedThePets(dog);
caretaker.feedThePets(cat);
}
}
interface SupperTime {
void feedMe();
}
class Caretaker {
public void feedThePets(SupperTime pet) {
pet.feedMe();
}
}
class Dog implements SupperTime {
public void feedMe() {
System.out.println("Oh boy, Kibbles n' Bits");
}
}
class Cat implements SupperTime {
public void feedMe() {
System.out.println("Yum. Purina Cat Chow");
}
}
I'm new to java and came across some advanced Enum code in my organization.
I am familiar with enums defined as follows: public enum someList { VALUE1, VALUE2, ..}
But in the advanced enum that I came across, a lot more seems to be going on..
/**
* Defines ways to compare records in {#link ApplicationStatus} to see if they
* are the same.
*/
public enum SourceStatusComparisonType
{
CONTEXT_KEY
{
#Override
public boolean compareKeys(SourceStatus source, ApplicationStatus status)
{
return source.getContextKey() == status.getSourceContextId();
}
},
DATE
{
#Override
public boolean compareKeys(SourceStatus source, ApplicationStatus status)
{
return source.getCobDate().equals(status.getCobDate());
}
};
public abstract boolean compareKeys(SourceStatus source,
ApplicationStatus status);
}
Could someone enlighten me on what's going on with the abstract method that's being overridden and overloaded. I don't understand what's going on in the above. I was told this is being used to abstract away if/else blocks from the code that uses it. But I'm not sure I understand.
Thank you in advance.
Each enum constants can be optionally followed by a class body, as specified in JLS §8.9.
The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes; in particular it cannot contain any constructors.
So, it is like creating an abstract class SourceStatusComparisonType, with an abstract method - compareKeys(). You create 2 instances of that class:
CONTEXT_KEY = new SourceStatusComparisonType() {
#Override
public void compareKeys() { .. }
};
DATE = new SourceStatusComparisonType() {
#Override
public void compareKeys() { .. }
};
And override the abstract method for each instances. You can't have any instance, without having an implementation of that method.
Similarly, all the enum constants, must provide an implementation of the abstract method in it's respective anonymous class body. Else, you can't have that method as abstract. You have to provide a body, which will be used by default for the enum constants, that don't provide that method implementation:
public enum SourceStatusComparisonType
{
CONTEXT_KEY
{
#Override
public boolean compareKeys(SourceStatus source, ApplicationStatus status)
{
return source.getContextKey() == status.getSourceContextId();
}
},
DATE
{
#Override
public boolean compareKeys(SourceStatus source, ApplicationStatus status)
{
return source.getCobDate().equals(status.getCobDate());
}
},
DEFAULT;
public boolean compareKeys(SourceStatus source,
ApplicationStatus status) {
// return somedefaultvalue;
}
}
So, for the first 2 constants, when you invoke compareKeys method, their specific implementation will be used, and for DEFAULT, the method defined outside will be used.
Enum type can have body like abstract methods and all enum constants should implement those abstract methods.
Look at the JSL here http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.2 and go to the section Example 8.9.2-4. Enum Constants with Class Bodies.
You even can implement an ENUM with an interface rather having a abstract method in it. Something like public enum SourceStatusComparisonType implements StatusComparable.
First thing you need to know about Enum in Java is that they are classes, think of them as classes. As such all the object oriented design/programming concepts you have learned apply to them.
Imagine a universe of your making, in which only 3 creatures exists, a Cat and Cow and a dog. One implementation would be
public abstract class Animal
{
private final String name;
public Animal(String name) {
this.name = name;
}
public abstract String sayHello(Animal other);
}
public class Cat extends Animal
{
public Cat(String name) {
super(name);
}
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Meow!!! to " + other.name);
}
}
public class Cow extends Animal
{
public Cow(String name) {
super(name);
}
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Moo!!! to " + other.name);
}
}
public class Dog extends Animal
{
public Dog(String name) {
super(name);
}
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Woof!!! to " + other.name);
}
}
public Universe {
public List<Animal> animals;
public Universe(List<Animal> animals) {
this.animals = animals;
}
public void run() {
for (Animal animal : animals) {
for (Animal other : animals) {
if (other != animal) {
animal.sayHello(other);
}
}
}
}
}
You are satisfied with your universe and deploy it in production. Only to find, another malicious developer has introduced a new creature to your universe, a zombie
public class Zombie extends Animal
{
public Zombie(String name) {
super(name);
}
public String sayHello(Animal other) {
System.out.println(this.name + " says Arrggggg!!! to " + other.name);
eat(other);
}
private void eat(Animal other) {
System.out.println("nomm nomm " + other.name);
}
}
Zombies in the guise of saying hello eat all the animals they run into. Pretty soon there will be no Cats, Dogs and Cows left. You decide to deploy version 2 of your universe to prevent this and guard against zombies
public Universe2 {
public List<Animal> animals;
public Universe(List<Animal> animals) {
this.animals = animals;
}
public void run() {
for (Animal animal : animals) {
for (Animal other : animals) {
if (other != animal) {
if (!(animal instanceof Zombie))
animal.sayHello(other);
}
}
}
}
}
You are happy now, but you notice the cat populations decreasing, turns out cats and dogs are no longer friends, the zombies have infected their brains, you deploy version 3 of the universe.
public Universe3 {
public List<Animal> animals;
public Universe(List<Animal> animals) {
this.animals = animals;
}
public void run() {
for (Animal animal : animals) {
for (Animal other : animals) {
if (other != animal) {
if (!(animal instanceof Zombie) && !((animal instanceof Cat) && (animal instanceof Dog)))
animal.sayHello(other);
}
}
}
}
}
In this version, there are no zombies. Cats and Dogs say hello to Cows but not each other. The universe is in equilibrium but you have a nagging feeling that not everything is right with the world. You sleep on it and come to the realization that you are open to attack similar to the zombie ones, you decide to lock down the universe and implement a rewrite to start from scratch and eliminate the zombie epidemic entirely. Except you like friendly zombies that say hello.
public Enum Animal {
Cat {
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Meow!!! to " + other.name);
}
},
Cow {
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Moo!!! to " + other.name);
}
},
Dog {
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Woof!!! to " + other.name);
}
},
Zombie {
#Override
public String sayHello(Animal other) {
System.out.println(this.name + " says Argggg!!! to " + other.name);
}
};
private String name;
public Animal(String name) {
this.name.
}
public String sayHello(Animal other);
}
public PerfectUniverse {
public List<Animal> animals;
public Universe(List<Animal> animals) {
this.animals = animals;
}
public void run() {
for (Animal animal : animals) {
for (Animal other : animals) {
if (other != animal) {
animal.sayHello(other);
}
}
}
}
}
You have eliminated the conditional if's using inheritance but locking down the extensibility by using an enumeration. You can do the same using classes, restricting access to Animal outside the package and making the derived types final. This of the enum Cat as an instance of Cat extending Animal. The derived types with overridden method avoid the need for instanceof checks with if conditionals.
Reusing a previous example, imagine a zoo where newly arriving animals have to be "processed" by the Zookeeper (think of checking them in to the zoo). Each animal's check-in process depends on its class taxonomy (Mammal, Bird, etc).
The processes differ because of the fundamental differences between the taxonomy classes - for example, Birds have wings, Mammals have teeth. You might also have some shared bits of the process which are true for all animals, but I've omitted them here.
Here is the code:
Animal.java
public interface Animal {
public AnimalProcessor<? extends Animal> getAnimalProcessor();
}
Mammal.java
public abstract class Mammal implements Animal {
#Override
public AnimalProcessor<Mammal> getAnimalProcessor() {
return new MammalProcessor();
}
// Specific to mammals
public abstract int getToothCount();
}
Bird.java
public abstract class Bird implements Animal {
#Override
public AnimalProcessor<Bird> getAnimalProcessor() {
return new BirdProcessor();
}
// Specific to birds
public abstract float getWingspan();
}
AnimalProcessor.java
public interface AnimalProcessor<T extends Animal> {
public void process(T critter);
}
MammalProcessor.java
public class MammalProcessor implements AnimalProcessor<Mammal> {
#Override
public void process(Mammal a) {
System.out.println("Tooth count is " + a.getToothCount());
}
}
BirdProcessor.java
public class BirdProcessor implements AnimalProcessor<Bird> {
#Override
public void process(Bird a) {
System.out.print("Wingspan is " + a.getWingspan());
}
}
Badger.java
public class Badger extends Mammal {
#Override
public int getToothCount() {
return 40;
}
}
Condor.java
public class Condor extends Bird {
#Override
public float getWingspan() {
return 2.9f;
}
}
ZooKeeper.java
import java.util.List;
public class ZooKeeper {
public void processNewAnimals(List<Animal> newcomers)
{
for(Animal critter : newcomers)
{
AnimalProcessor<? extends Animal> ap = critter.getAnimalProcessor();
// This line has a compilation error!
ap.process(critter);
}
}
}
MainClass.java
import java.util.LinkedList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ZooKeeper keeper = new ZooKeeper();
List<Animal> animals = new LinkedList<Animal>();
animals.add(new Badger());
animals.add(new Condor());
keeper.processNewAnimals(animals);
}
}
There are no warnings anywhere, but ap.process(critter) cannot compile.
I know this is because AnimalProcessor<Bird> is not of type AnimalProcessor<Animal> but I cannot see how resolve the issue. The call to <T extends Animal> getAnimalProcessor() will return a suitable AnimalProcessor<T extends Animal>, but I cannot express this in the code.
Perhaps I should not be pulling the AnimalProcessor out in the first place?
The goal is, of course, to be able to add Reptile, without changing the core.
public interface Animal<THIS extends Animal<THIS>> {
AnimalProcessor<THIS> getAnimalProcessor();
}
public abstract class Mammal implements Animal<Mammal> {
A little like Enum<E extends Enum<E>>.
It may be a better idea to remove the method.
AnimalProcessor.java :
public interface AnimalProcessor<T extends Animal> {
public void process(Animal critter);
}
MammalProcessor.java
public class MammalProcessor implements AnimalProcessor<Mammal> {
#Override
public void process(Animal a) {
System.out.println("Tooth count is " + ((Mammal)a).getToothCount());
}
}
I have a collection of Animal objects.
My core code wants to treat all of these as Animals, all the same. Each Animal needs to be processed in some way. The nature of the processing depends on the sub-type of the Animal (bird, mammal, etc).
My code currently looks as follows.
public interface Animal {
public String getTaxonomyClass();
}
public abstract class Bird implements Animal {
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public abstract float getWingspan();
}
public abstract class Mammal implements Animal {
#Override
public String getTaxonomyClass() {
return "mammalia";
}
// Specific to mammals
public abstract int getToothCount();
}
public interface AnimalProcessor {
public String getSupportedTaxonomyClass();
public void process(Animal a);
}
public class MammalProcessor implements AnimalProcessor {
#Override
public String getSupportedTaxonomyClass() {
return "mammalia";
}
#Override
public void process(Animal a) {
System.out.println("Tooth count is " + ((Mammal)a).getToothCount());
}
}
public class BirdProcessor implements AnimalProcessor {
#Override
public String getSupportedTaxonomyClass() {
return "aves";
}
#Override
public void process(Animal a) {
System.out.print("Wingspan is " + ((Bird)a).getWingspan());
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ZooKeeper {
Map<String, AnimalProcessor> registry = new HashMap<String, AnimalProcessor>();
public void registerProcessor(AnimalProcessor ap)
{
registry.put(ap.getSupportedTaxonomyClass(), ap);
}
public void processNewAnimals(List<Animal> newcomers)
{
for(Animal critter : newcomers)
{
String taxonomy = critter.getTaxonomyClass();
if(registry.containsKey(taxonomy))
{
// if I can process the animal, I will
AnimalProcessor ap = registry.get(taxonomy);
ap.process(critter);
}
}
}
}
import java.util.LinkedList;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
ZooKeeper keeper = new ZooKeeper();
keeper.registerProcessor(new MammalProcessor());
keeper.registerProcessor(new BirdProcessor());
List<Animal> animals = new LinkedList<Animal>();
animals.add(new Mammal() { // badger
#Override
public int getToothCount() {
return 40;
} }
);
animals.add(new Bird() { // condor
#Override
public float getWingspan() {
return 2.9f;
} }
);
keeper.processNewAnimals(animals);
}
}
Generally this is easy to understand and works nicely! I can add plug-in new processors and animal types at my leisure without changing the ZooKeeper class or any of the interfaces. You can imagine a more advanced main class, loading the Animals from a database, and processing them all in turn.
However, I worry about the downcasts inside the AnimalProcessor subclasses! This strikes me as something which should not be there, and may be a violation of OO principles. After all, at the moment I can pass a Bird to a MammalProcessor's process() method, and there will be a ClassCastException.
Can anyone suggest a design pattern to solve this? I looked at the Visitor pattern, but couldn't quite figure out how to apply it in this case! The key is to make the core code (ZooKeeper) treat all animals the same, and make it so that support for new Animals can be added trivially. Thanks!
I would suggest the following:
public interface Animal {
public AnimalProcessor<? extends Animal> getProcessor();
}
so each animal will return it's matching processor.
public interface AnimalProcessor<T extends Animal> {
public void process(T a);
}
so the processors will be typed with their matching type it's should process.
so the implantation will be like this:
public abstract class Bird implements Animal {
private BirdProcessor processor = new BirdProcessor();
public abstract float getWingspan();
#Override
public AnimalProcessor<Bird> getProcessor() {
return processor;
}
}
public class BirdProcessor implements AnimalProcessor<Bird> {
#Override
public void process(Bird b) {
System.out.print("Wingspan is " + b.getWingspan());
}
}
This is where generics work great.
First, you need to make AnimalProcessor generic:
public interface AnimalProcessor <T extends Animal> {
public String getSupportedTaxonomyClass();
public void process(T a);
}
Next, in your specific processors, you specify the generic type - eg for mammals:
public class MammalProcessor implements AnimalProcessor<Mammal> {
public String getSupportedTaxonomyClass() {
return "mammalia";
}
public void process(Mammal a) {
System.out.println("Tooth count is " + a.getToothCount());
}
}
Now, the process method only accepts Mammal objects, no birds here.
I suggest the following :
public interface Animal {
public String getTaxonomyClass();
public void process();
}
Now each animal class implementing Animal should implement its own processing logic.
For example :
public class Bird implements Animal {
public Bird(float wingSpan) {
this.wingSpan = wingSpan;
}
#Override
public String getTaxonomyClass() {
return "aves";
}
#Override
public void process() {
System.out.print("Wingspan is " + wingSpan);
}
// Specific to birds
private float wingspan;
}
Now you can have only one AnimalProcessor which processes as follows :
public void process(Animal a) {
a.process();
}
Make you AnimalProcessor generic;
public interface AnimalProcessor<T extends Animal> {
public String getSupportedTaxonomyClass();
public void process(T a);
}
public class MammalProcessor implements AnimalProcessor<Mammal> {
#Override
public String getSupportedTaxonomyClass() {
return "mammalia";
}
#Override
public void process(Mammal a) {
System.out.println("Tooth count is " + a.getToothCount());
}
}
So you've got a class like this...
public abstract class Bird implements Animal {
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public abstract float getWingspan();
}
All Birds will have a wingspan, even if the wingspan is 0. So, why don't you change the class to something like this...
public class Bird implements Animal {
float wingspan = 0.0f;
public Bird(float wingspan){
this.wingspan = wingspan;
}
#Override
public String getTaxonomyClass() {
return "aves";
}
// Specific to birds
public float getWingspan(){
return wingspan;
}
}
So, to create a new Bird, instead of doing this...
animals.add(new Bird() { // condor
#Override
public float getWingspan() {
return 2.9f;
} }
);
You would just do this...
animals.add(new Bird(2.9f)); // condor
This would seem to make the whole thing a lot simpler and nicer for your purposes. You would do a similar change for your Mammal class too.
Now, for the processing of animals... if all Animals are going to be processed, you could just implement process() in Bird rather than needing a separate BirdProcessor class. To do this, in Animal, declare a method public void process();. Your Bird would implement it like this...
public void process() {
System.out.print("Wingspan is " + getWingspan());
}
and you would change your AnimalProcessor to simply do this (note: no longer an interface)...
public class AnimalProcessor {
public void process(Animal a) {
a.process();
}
}
Your AnimalProcessor class would then be able to handle all Animals.
Alternatively, if you want to leave AnimalProcessor as it is, it would probably be good to change the following though, to avoid the ClassCastException (this code here is for the BirdProcessor)...
public void process(Animal a) {
if (a instanceof Bird){
System.out.print("Wingspan is " + ((Bird)a).getWingspan());
}
}
Is this kinda what you were looking for?
Your problem are methods such as
public abstract int getToothCount();
...aren't defined in Animal. Instead, they are defined in specific subclasses of Animal. This means you can't treat Animals generically since they are fundamentally different.
To overcome this, the one approach would be to create abstract methods for all these in the Animal class.
Bird might respond to getToothCount() with "0".
Since all animals could respond to getWingspan(), getTootCount(), etc, you would not have to perform any type-specific checking. If this isn't good enough, create abstract implementations of "boolean hasWings()", "boolean hasTeeth()" etc etc in Animal.
Now you could say, for some animal a:
if (a.hasWings()) System.out.println("My wingspan is "+a.getWingSpan());
which would work for any animal. Of course, each subclass of Animal would have to implement all the various methods.
Another option is to add non-abstract methods to Animal. These methods would supply default answers. For example, getWingSpan() would return 0, getToothCount() would return 0, etc. Shark would override getToothCount(). Eagle would override getWingSpan()...
Then your subclasses would only have to override (or even know about) methods related directly to them.
I designed the following for a problem:
class Animal {
// ...
}
class Guppy extends Animal { ... }
class Pigeon extends Animal { ... }
class TailedAnimal extends Animal {
// ...
}
class Dog extends TailedAnimal { ... }
class Cat extends TailedAnimal { ... }
class HornedAnimal extends Animal {
// ...
}
class Ram extends HornedAnimal { ... }
public static void main(String[] args) {
Animal a = getSomeAnimal();
a.doSomething();
if (a instanceof TailedAnimal) {
// do something
}
if (a instanceof HornedAnimal) {
// do something else
}
}
Animal, HornedAnimal and TailedAnimal are used mainly as data models.
Since Java does not support multiple inheritance, I have trouble creating Rhinoceros which is a Horned and Tailed animal. After asking around, someone recommended using composition and interfaces. I came up with the following:
class Animal {
// ...
}
class Guppy extends Animal { ... }
class Pigeon extends Animal { ... }
class Ram extends Animal implements IHorned { ... }
class Cat extends Animal implements ITailed { ... }
class Dog extends Animal implements ITailed {
BasicTail t = new BasicTail();
public Object getTail() {
return t.getTail();
}
public void setTail(Object in) {
t.setTail(in);
}
}
interface ITailed {
public Object getTail();
public void setTail(Object in);
//...
}
class BasicTail implements ITailed {
Object myTail;
public Object getTail() { return myTail; }
public void setTail(Object t) { myTail = t; }
}
interface IHorned {
// getters and setters
}
public static void main(String[] args) {
Animal a = getSomeAnimal();
a.doSomething();
// how do I check if a is horned or tailed?
}
My interface has getters and setters. Is there any way to avoid this? Assuming that there is currently no way to abstract the behaviour of Tails and Horns, and they're are being used mainly as data holders. How do I determine if my Animal is Horned or Tailed?
I'd suggest strategy pattern here. In short:
interface TailedAnimal {
void moveTail();
}
interface HornedAnimal {
void hitWithHorn();
}
class Rhinoceros() implements TailedAnimal, HornedAnimal {
private TailedAnimal tail; //Instantiate it somehow e.g. constructor, setter
private HornedAnimal horn; //Instantiate it somehow e.g. constructor, setter
public void moveTail() {
tail.moveTail();
}
public void hitWithHorn() {
horn.hitWithHorn();
}
}
By using this you encapsulate behavior in a concrete implementation of the interfaces, and may easily share exactly the same behavior for a few animals, as well as change it at run-time.
I think you must avoid setters in general. If you can, use immutable objects, and initialize its private data into its constructor.
To distinguish animals, I used another pattern, the visitor one. It's verbose, but you don't have to test directly what animal you're processing.
public class Animals {
private Animals() {
}
interface Animal {
void accept(final AnimalProcessor visitor);
}
interface AnimalProcessor {
void visitTailed(final TailedAnimal tailedAnimal);
void visitHorned(final HornedAnimal hornedAnimal);
}
interface TailedAnimal extends Animal {
void moveTail();
}
interface HornedAnimal extends Animal {
void hitWithHorns();
}
static class Dog implements TailedAnimal {
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
}
}
static class Cat implements TailedAnimal {
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
}
}
static class Ram implements HornedAnimal {
public void hitWithHorns() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitHorned(this);
}
}
static class Rhinoceros implements HornedAnimal, TailedAnimal {
public void hitWithHorns() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
visitor.visitHorned(this);
}
}
public static void main(String[] args) {
Collection<Animal> animals = new ArrayList<Animal>(Arrays.asList(new Dog(), new Cat(), new Rhinoceros()));
for (final Animal animal : animals) {
animal.accept(new AnimalProcessor() {
public void visitTailed(final TailedAnimal tailedAnimal) {
// you do what you want when it's a tailed animal
}
public void visitHorned(final HornedAnimal hornedAnimal) {
// you do what you want when it's a horned animal
}
});
}
}
}
I've edited out my previous answer. I thought of something much better. See the revision for this post if you're curious.
Make use of the Specification pattern. It very much fits the bill here - way more than Decorator. You asked to "check" if an Animal was horned. Decorator pattern delivers transparency, while in this situation you appear to be asking for discrimination.
The Specification pattern encapsulates knowledge of how to evaluate some criteria. In our case, we'd want something like:
public interface Specification {
public boolean isSatisfiedBy(Animal aCriteria);
}
public class HornedAnimalSpecification implements Specification {
#Override
public boolean isSatisfiedBy(Animal aCriteria) {
//Right here is where the heart of your problem
//can be solved.
//
//Reserved conquering grounds.
}
}
Now you can define your Animal hierarchy however you want. The only thing you now need to do is figure out what makes an animal horned. Your answer to that question goes into the Specification class. Then your main function is easy as pie.
public class Zoo {
public static void main(String[] args) {
Animal ram = getHornedAnimal(); //Instantiate however you'd like.
Specification specification = new HornedAnimalSpecification();
if (specification.isSatisfiedBy(ram)) {
//Bingo, it's horned.
} else {
//Not horned!
}
}
}