Builder

Excuse the simplicity of this question.

I want to employ generics for a Factory.

For example, suppose I have an Animal interface, and then the usual animal implementations: a Dog, a Cat etc.

Creating my animal objects is quite complex (involves database calls etc) so I have decided to have a Builder class which has a number of static builder methods for making them.

For example:

publicstatic Dog createDog()

publicstatic Cat createCat() etc.

The creation code is quite similar for all the animals it's the return type that is key different part.

I could of course have a

publicstatic Animal createAnimal(class animalType)

but then I would have loads of ugly casting in my code, So I thought generics could help me out here,

I tried something like:

publicstatic <T> Animal <T> createEntity(T type)

however the Animal interface is not a generic.

Ok, I am confused as how do something I thought would be simple?

Should I make the Animal interface a generic or if not can I use any of the generica interfaces to help me out.

[1436 byte] By [beginner2a] at [2007-11-26 23:59:04]
# 1

You mean something like this?

class Main {

public static void main(String[] args)

throws InstantiationException, IllegalAccessException {

Cat cat = AnimalFarm.getAnimal(Cat.class);

Dog dog = AnimalFarm.getAnimal(Dog.class);

System.out.println("cats say: "+cat.makeSound());

System.out.println("dogs say: "+dog.makeSound());

}

}

class AnimalFarm {

static <T> T getAnimal(Class<T> type)

throws InstantiationException, IllegalAccessException {

Object obj = type.newInstance();

return type.cast(obj);

}

}

interface Animal {

public String makeSound();

}

class Dog implements Animal {

public String makeSound() { return "Wooff!"; }

}

class Cat implements Animal {

public String makeSound() { return "Meoww!"; }

}

prometheuzza at 2007-7-11 15:47:36 > top of Java-index,Core,Core APIs...
# 2
Yeah that works, I am interesting that you still have to call the .cast().Thanks for your time.
beginner2a at 2007-7-11 15:47:37 > top of Java-index,Core,Core APIs...
# 3

No you do not. The following code also works:class AnimalFarm {

static <T> T getAnimal(Class<T> type) throws InstantiationException, IllegalAccessException {

return type.newInstance();

}

}

raindropa at 2007-7-11 15:47:37 > top of Java-index,Core,Core APIs...
# 4

May I suggest a minor tweak:

class AnimalFarm {

static <T extends Animal> T getAnimal(Class<T> type) throws InstantiationException, IllegalAccessException {

return type.newInstance();

}

}

This ensures that you only construct animals with the getAnimal method.

PeterAhea at 2007-7-11 15:47:37 > top of Java-index,Core,Core APIs...
# 5

> Yeah that works, I am interesting that you still have

> to call the .cast().

Good call! As raindrop already posted: you don't have to.

Also note the change PeterAhe proposed: he is right, you should restrict T to be of type Animal.

> Thanks for your time.

You're welcome.

; )

prometheuzza at 2007-7-11 15:47:37 > top of Java-index,Core,Core APIs...