M-D
M-D

Reputation: 10397

Class Design issues

Currently, I have the following class design :

  1. AnimalHandler (A base class for all animals)

  2. TigerHandler extends AnimalHandler, LionHandler extends AnimalHandler, LeopardHandler extends AnimalHandler and so on.

  3. TigerHandler, LionHandler, LeopardHandler etc "HAS A" Tiger, Lion, Leopard and so on.

Now the issue is : TigerHandler, LionHandler, LeopardHandler etc are all the same(they have the same methods, etc) except that they deal with Tiger,Lion, Leopard, etc classes respectively. So if I need a new Animal group called Cheetah, I just need to copy any of the (Tiger|Lion|Leopard)Handler and do a search and replace of its "HAS A" class names like Tiger,Lion, Leopard.

Instead of copying/creating a new CheetahHandler as above, is there a way (design) that I can use ? Like, say, a GenericHandler that can deal with any of these "HAS A" classes (like Tiger,Lion, Cheetah, etc).

Upvotes: 0

Views: 236

Answers (4)

BobTheBuilder
BobTheBuilder

Reputation: 19304

Using generics is a much better approach.

Make sure all animal classes (Tiger, Lion, Leopard) extends lets say an Animal class. Then use:

public class AnimalHandler<A extends Animal> {
    private A animal;
}

And use animal inside your code.

When you want to instantiate a tiger handler, use it as:

AnimalHandler<Tiger> tigerHandler = new AnimalHandler<>();

Upvotes: 2

Ray Stojonic
Ray Stojonic

Reputation: 1270

As others have noted, generics are a great approach, or you could use a Strategy pattern:

   class Animalhandler {
      List<Animal> animals;
      // add, remove and do stuff with animals
   }

    interface Animal {
        void makeSound();
    }
    class Cat extends Animal {
        public void makeSound() {
        // meow
        }
    }

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68443

A bit of details in terms of how each of Tiger,Lion, Leopard classes are used in each of the handler

-- If the method interfaces are all the same, and only internal functionality is different

-- if there is extension on Animalhandler you are doing inside the childHandlers

Then, then you should make AnimalHandler an abstract class (implementing iAnimalHandler) and use generics to process the Animal (Super class for each Tiger,Lion, Leopard )

Upvotes: 0

NilsH
NilsH

Reputation: 13831

If all your handlers do is provide type safety, and does nothing else, you can use generics instead:

public class AnimalHandler<T extends Animal> {

    private T theAnimal;

    public T getTheAnimal() {} 

    // Etc

}

Upvotes: 4

Related Questions