是否可以找到对象的实例而不将其传递给方法?
我有一个解析器类“MessageParser”,我向它传递一条“String”类型的消息以供解析。该类的解析方法签名是
public void parse(String message);
我需要将“属性”的实例传递给它,但我不想更改该方法的签名以向其添加新参数。在过去的几天里,我一直在努力解决这个问题,并尝试了几种选择 - 请参阅 发送对象类型的对象而不是 String - 多态性
调用解析方法“ParserManager”的类知道属性对象。有没有办法让 MessageParser 找到属性对象而不将其传递给它?
编辑
这里是一些示例代码。 我希望“MessageCparser”访问“ParserManager”中的“prop”对象,而不更改“Parser”接口或“ParserManager”类中的任何内容。这可能吗?
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);
}
}
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最明显的解决方案是添加对
Properties
对象的引用作为ParserManager
中的字段,然后为ParserManager
提供属性对象作为构造函数参数或通过 setter 方法,如下所示:看看您之前的问题,我想说如果您在
Parser
中添加setParsingProperties
就可以了代码>接口。对于那些不需要属性的解析器,该方法可以实现为空方法。关于您的编辑:不,不可能这样解决它。
仅当
MessageObject
是String
的子类型时才有效(但由于String
是最终的(无法扩展),因此不可能是这种情况) 。肮脏的快速修复是检查(使用instanceof)Parser是否是MessageCParser的实例并强制转换它,然后使用< code>MessageCParser 特定解析方法,将
Properties
作为参数。The most evident solution would be to add a reference to the
Properties
object as a field in theParserManager
, and then either provide theParserManager
with 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
setParsingProperties
in theParser
interface. 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
MessageObject
is a subtype ofString
(but sinceString
is final (can't be extended) that cannot be the case).The dirty quick-fix would be to check (with
instanceof
) if theParser
is an instance ofMessageCParser
and cast it and then use aMessageCParser
specific parse method that takes theProperties
as an argument.嗯,从广义上讲,有四种获取信息的方法:
所有这些当然都可以间接使用 - 例如,如果有什么else知道有关
Properties
的信息可通过上述机制之一获得,然后您可以访问该机制,然后了解有关Properties
的信息。但您不能仅仅找出调用对象并询问该对象。。您需要如何处理相关的
Properties
?对于同一MessageParser
,Properties
是否会因每次调用而有所不同?如果是这样,它确实应该是一个参数。您说您不想更改方法的签名 - 但如果您想传递更多信息,这正是您应该做的。Well, there are four ways of getting information, broadly speaking:
All of these can be used with indirection of course - for example, if something else which knows about the
Properties
is available via one of the above mechanisms, then you can get to that and then find out about theProperties
. But you can't just find out the calling object and ask that.What do you need to do with the
Properties
in question? Would theProperties
vary on a call-by-call basis for the sameMessageParser
? If so, it really should be a parameter. You say you don't want to change the signature of the method - but if you want to pass more information in, that's exactly what you should do.如果您的“Properties”对象只有一个实例,则可以使用类似 单例模式 的内容。
您还可以在 MessageParser 中的某处添加对 ParserManager 的引用,然后调用 ParserManager 上的方法,该方法将返回属性,但如果没有一些代码片段,就不可能为您提供更多帮助。
If you have only one instance of your "Properties" object, you can use something like the Singleton pattern.
You can also add a reference to the ParserManager somewhere in your MessageParser and then call a method on ParserManager which will return the Properties, but without some code snippets it's impossible to help you further than this.