I have a parser class “MessageParser” which i pass a message which is of type “String” to it for it to be parsed. The parsing method signature for the class is
public void parse(String message);
I need to pass an instance of “Properties” to it but i dont want to change the signature of the method to add a new argument to it. I have been struggling with this for the last couple of days and have tried a couple of options – see Sending in an object of type Object instead of String – Polymorphism
The class that calls the parsing method “ParserManager” knows of the properties object. Is there a way for the MessageParser to find the properties object without it being passed to it?
Edit
Here is some example code.
I would like the “MessageCparser” to access the “prop” object in “ParserManager” without changing anything in the “Parser” interface or the “ParserManager” class. Is this possible?
public interface Parser{
public void parse(String message);
}
public class MessageCParser implements Parser{
public void parse(String message){
MessageObject mobject = (MessageObject)message;
System.out.println("Parsing C" + mobject.getMessage());
}
public void parse(String m){}
}
import java.util.HashMap;
public class ParserManager{
Properties prop = null;
public ParserManager() {
prepare();
prop = new Properties()
}
HashMap parsers = new HashMap();
public void prepare(){
parsers.put("A",new MessageCParser());
}
public void parseMessage(String msgType, String message){
((Parser)parsers.get(msgType)).parse(message);
}
}
Thanks
The most evident solution would be to add a reference to the
Propertiesobject as a field in theParserManager, and then either provide theParserManagerwith the properties object as a constructor argument or through a setter-method as shown below:Looking at your previous question, I’d say it would be fine if you added a
setParsingPropertiesin theParserinterface. The method can be implemented as an empty method for those parser that don’t need the properties.Regarding your edit: No, it’s not possible to solve it like that.
Will only work if
MessageObjectis a subtype ofString(but sinceStringis final (can’t be extended) that cannot be the case).The dirty quick-fix would be to check (with
instanceof) if theParseris an instance ofMessageCParserand cast it and then use aMessageCParserspecific parse method that takes thePropertiesas an argument.