The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Reflection on Tiger
Subject:  A generic Factory
Date:  2004-12-07 00:27:56
From:  cilipini
Response to: A generic Factory


The following code works:

class Factory<T> {
private Class<T> c;

public Factory( Class<T> c ) {
this.c = c;
}

public T giveNew() throws Exception {
return c.newInstance();
}
}

Assume you have a class
class Product {
public String name = "mp3-player";
}
you can do:
Factory<Product> myFactory =
new Factory<Product>( Product.class );
System.out.println(
"Name: " + myFactory.giveNew().name );
will return "Name: mp3-player"

 Feed java.net RSS Feeds