Generics and prototype patter

Hello, I'm trying to do the following (see C# example code):

http://en.wikipedia.org/wiki/Prototype_pattern

This is (bad) a try in my code:

import......

publicclass DataFactory{

private DataFactory(){}

privatestatic HashMap<Type, Abstractdata><?>> instances =new HashMap<Type, Abstractdata><?>>();

static{

instances.put(Types.USER,Userdata.getInstance());

instances.put(Types.GROUP,Groupdata.getInstance());

}

publicstatic Abstractdata<?> getData(Type type){

return instances.get(type);

}

publicstaticenum Type{

USER, GROUP;

}

}

I also have singleton data classes (Userdata and Groupdata) that extend Abstractdata<User> and Abstractdata<Group> respectively. This is Abstractdata

import........

publicabstractclass Abstractdata<E>implements Observer{

protected LinkedList<E> data;

protectedstatic ConnectionPool cpp;

protected Abstractdata(){

cpp = ConnectionPool.getInstance();

this.data = this.getDataList();

}

publicabstractvoid add(E element)throws Exception;

publicabstractvoid update(Observable o, Object arg);

publicabstractvoid remove(E element)throws Exception;

protectedabstract LinkedList<E> getDataList();

public LinkedList<E> getData(){return this.data;}

public E get(int index){return this.data.get(index);}

publicint size(){return this.data.size();}

}

If I need some userdata, I do this:

Abstractdata<User> ud = DataFactory.getData(DataFactory.Types.USER);

but then I get an error saying:

Type mismatch: cannot convert from Abstractdata<capture-of ?> to Abstractdata<User>

How can I avoid this error?

[3781 byte] By [BigLebowskia] at [2007-10-2 13:43:20]
# 1

Insert a cast.

That's not going to solve the more important problem. You're not really using the Prototype pattern as far as I can tell. You create an instance, put it in a HashMap, then whenever someone asks for an instance you return that instance. You're not using a prototype to create clones, you're just returning the same instance every time.

kablaira at 2007-7-13 11:38:40 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Thanks Kablair, but where exactly do I have to insert the cast? Anyhow I guess there has to be a solution without the need of a cast. What I want to do is exactly as you described. I store instances in my hashmap and the return an instance based on the type that is requested.

This would be the desired result:

Abstractdata<User> udata = DataFactory.getType(Type.USER);

Abstractdata<Group> gdata = DataFactory.getType(Type.GROUP);

Is there no way to do get this effect by (mis)using the prototype pattern? Maybe another way? Suggestions are welcome.

BigLebowskia at 2007-7-13 11:38:40 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
I'm sorry for bumping this up again, but it would be really nice to have a solution for this. Perhaps this fits better in the "generics" forum... anyway, any one else cares to share his thoughs on this subject?
BigLebowskia at 2007-7-13 11:38:40 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

http://www.developer.com/net/net/article.php/626441

http://www.allapplabs.com/java_design_patterns/prototype_pattern.htm

http://www.fluffycat.com/Java-Design-Patterns/Prototype/

http://www.jguru.com/faq/view.jsp?EID=255004

http://www.javaperformancetuning.com/tips/patterns.shtml

http://www.c-sharpcorner.com/Language/PrototypePatternsinCSRVS.asp

mchan0a at 2007-7-13 11:38:40 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
None of those links supplied information about my specific problem... generics in combination with the data factory.
BigLebowskia at 2007-7-13 11:38:40 > top of Java-index,Other Topics,Patterns & OO Design...