Hello,
I have several classes which work as wrappers over some heavy libs, such as parsers, taggers, and other resources. All these libs have one thing in common: they have a load/init method which takes a serialized model (a large file) as input, and return a class that can be used to do concrete things such as parsing text.
To illustrate this situation, lets say I have a ConcreteParser lib to do parsing, for which I have created the following class to wrap it:
public class ParserWrapper {
private static final ConcreteParser parser = null;
private static void init(String modelFileName) {
if (parser == null)
parser = ConcreteParser.load(modelFileName);
}
public static Result parse(input) {
if (parser == null) throw new RuntimeException(...);
return parser.parse(input);
}
}
As you can see, this requires the client to first invoke init before parse, which is definitely a no-no. I have also tried a solution based on the Singleton pattern, but I still think there's a better way to do what I want.
The ConcreteParser is static because every model takes a relatively long time to load, and consume lots of memory. Therefore, I need them to be shared among every class that uses it.
So, my question is: is there another (more elegant) way to do what I want? How can I improve this code? I thought of creating a class ResourceManager, that had several methods like "createParser" and "createTagger", in order to have a single point to load resources. This class would also check if each resource had already been instantiated, et cetera. Any thoughs on these?
Regards.