I've recently learned of MVC (model view controller) and am trying to refactor an existing program. I am in a situation where I'd like to have exactly one object of a particular class so it seemed evident that I should use a singleton. This singleton will be used to keep a hashmap of certain things. However this hashmap will have to be initialized when the singleton is created by reading nodes from a xml file and storing them as class objects.
Now my dilemma is:
If I initialize the xml attributes (reading and converting to class objects), I'm effectively doing things in the model class that the controller should be doing, which is definitely not good.
If I put the initialization method in a controller class, I would have to refer to a controller class from a model class, which does not conform to the MVC design.
If I put the whole singleton in controller, then I'd have to look for the hashmap in the controller package, which defeats the purpose of having model classes.
I can't pass a premade hashmap to the singleton as a parameter neither, because the constructor is private.. Well, I technically could, by giving it to the getInstance() method as a parameter, but it feels like a dirty way of fixing, since I now either pass null every time, or make another getInstance() method that doesn't accept a parameter.
Right now, my code is looking like this:
public class CategoryCatalog{
private static CategoryCatalog categoryCatalog;
private HashMap<CarOptionCategory, Set<ICarOption>> categoryOptionsMap;
private CategoryCatalog(){
categoryOptionsMap = new HashMap<CarOptionCategory, Set<ICarOption>>();
initialize();
}
public static CategoryCatalog getInstance(){
if(categoryCatalog == null){
categoryCatalog = new CategoryCatalog();
}
return categoryCatalog;
}
private void initialize(){
// TODO: xml
}
}
Am I overlooking something or should I use a different approach?