How do I create a class repository in Java and do I really need it?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-18T09:50:32Z Indexed on 2010/03/18 10:01 UTC
Read the original article Hit count: 258

Filed under:
|
|
|

I have a large number of objects which are identified by names (strings). So, I would like to have a kind of mapping from object name to the class instances.

I was told that in this situation I can use a "repository" class which works like that:

Server myServer = ServerRepository.getServer("NameOfServer");

So, if there is already an object (sever) with the "NameOfServer" it will be returned by the "getServer". If such an object does not exist yet, it will be created and returned by the "getServer".

So, my question is how to program such a "repository" class? In this class I have to be able to check if there is an instance of a given class such that it has a given value of a given field. How can I do it? I need to have a kind of loop over all existing object of a given class?

Another part of my question is why I cannot use associative arrays (associative container, map, mapping, dictionary, finite map)? (I am not sure how do you call it in Java) In more details, I have an "array" which maps names of objects to objects. So, whenever I create a new object, I add a new element to the array: myArray["NameOfServer"] = new Server("NameOfServer").

© Stack Overflow or respective owner

Related posts about java

Related posts about oop