I have legacy code, similar to this
private Resource getResource(String type){
Resource resource = null;
if("A".equals(type)){
resource = Utils.getResourceA(); //static func
}else if("B".equals(type)){
resource = Utils.getResourceB(); //static func
}else if("C".equals(type)){
resource = Utils.getResourceC(); //static func
}else if // etc..
}
As you can see it's something that will be hard to maintain for new types...
What is the best practice to solve that?
I was thinking of creating Class for each resource function that will implement same interface.
Then create a Map<String,IResource>
key is the type and IResource will be the instance of class.
But there are problems with this solution.
- Where to create this map? inside getResource() method? inside class that holds this method?
- I will be holding instances of classes in memory in cases where I won't be using many of them
- Instead of simple static method, I will now have a full object (for each method) !
A note: I am not using any frameworks, pure java.
Thanks