This is my problem: I'm writing a Java library that allows to process a specific type of message in one of two possible ways (but potentially the client may add other processing modalities). I've created the following classes, according to the strategy pattern:
interface Processor {
Message processMessage();
}
class ProcessorOne<M extends Message> implements Processor{
M processMessage(){ //do stuff }
}
class ProcessorTwo<M extends Message> implements Processor{
M processMessage(){ //do other stuff }
}
abstract class Message{
}
//example
class MessageTypeOne extends Message{
}
//example
class MessageTypeTwo extends Message{
}
Now, the Client uses a configuration in such a way that for every concrete type of Message he says: "MessageTypeOne: ProcessorOne, MessageTypeTwo: ProcessorTwo, MessageTypeThree: ProcessorOne", establishing that messages of type MessageTypeOne have to processed by class ProcessorOne and so on.. The configuration could be a xml file or any other type of configuration, this is up to the client.
My question is: How can I create a "link" between what Client will read from configuration (using its own way of parsing, according to the configuration type he has chosen) and the instantiation of the concrete processors using a Factory pattern?
Thanks in advance
I think it's difficult to archive that. You can create processor instance based on their name through Factory pattern, but cannot associate these two class in config file. I think the easist way is use different Factory for your business logic. For example:
Map<String, String> conf = null; // What parsed from config
String messageType = // What you have
Processor processor = ProcessorFactory.createProcessor(conf.get(messageType));
Message message = MessageFactory.createMessage(messageType);
// processing