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 (server) 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")
.