是否可以找到对象的实例而不将其传递给方法?

发布于 2024-10-19 22:55:23 字数 1326 浏览 3 评论 0原文

我有一个解析器类“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2024-10-26 22:55:23

最明显的解决方案是添加对 Properties 对象的引用作为 ParserManager 中的字段,然后为 ParserManager 提供属性对象作为构造函数参数或通过 setter 方法,如下所示:

class ParserManager {
    ...
    Properties props;

    public void setParsingProperties(Properties props) {
        this.props = props;
    }

    public void parse(String message) {
        // props available here, without being passed as agurment.
    }
}

class CallingParserManager {
    ...
    void someMethod() {
        ...
        parserManager.setParsingProperties(propertiesToUse);
        parserManager.parse(theString);
        ...
    }
    ...
}

看看您之前的问题,我想说如果您在 Parser 中添加 setParsingProperties 就可以了代码>接口。对于那些不需要属性的解析器,该方法可以实现为空方法。


关于您的编辑:不,不可能这样解决它。

MessageObject mobject = (MessageObject) message;

仅当 MessageObjectString 的子类型时才有效(但由于 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 the ParserManager, and then either provide the ParserManager with the properties object as a constructor argument or through a setter-method as shown below:

class ParserManager {
    ...
    Properties props;

    public void setParsingProperties(Properties props) {
        this.props = props;
    }

    public void parse(String message) {
        // props available here, without being passed as agurment.
    }
}

class CallingParserManager {
    ...
    void someMethod() {
        ...
        parserManager.setParsingProperties(propertiesToUse);
        parserManager.parse(theString);
        ...
    }
    ...
}

Looking at your previous question, I'd say it would be fine if you added a setParsingProperties in the Parser 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.

MessageObject mobject = (MessageObject) message;

Will only work if MessageObject is a subtype of String (but since String is final (can't be extended) that cannot be the case).

The dirty quick-fix would be to check (with instanceof) if the Parser is an instance of MessageCParser and cast it and then use a MessageCParser specific parse method that takes the Properties as an argument.

丑丑阿 2024-10-26 22:55:23

嗯,从广义上讲,有四种获取信息的方法:

  • 它可以是调用该方法的对象状态的一部分(例如,它可以传递给构造函数,然后保留在字段中)
  • 它可以是参数方法本身的
  • 它可以静态访问,例如通过单例
  • 它可以通过线程局部变量访问

所有这些当然都可以间接使用 - 例如,如果有什么else知道有关 Properties 的信息可通过上述机制之一获得,然后您可以访问该机制,然后了解有关 Properties 的信息。但您不能仅仅找出调用对象并询问该对象。

您需要如何处理相关的Properties?对于同一 MessageParserProperties 是否会因每次调用而有所不同?如果是这样,它确实应该是一个参数。您说您不想更改方法的签名 - 但如果您想传递更多信息,这正是您应该做的。

Well, there are four ways of getting information, broadly speaking:

  • It could be part of the state of the object that the method is called on (e.g. it could be passed to the constructor and then retained in a field)
  • It can be a parameter of the method itself
  • It could be accessed statically, e.g. via a singleton
  • It could be accessed via a thread-local variable

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 the Properties. 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 the Properties vary on a call-by-call basis for the same MessageParser? 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.

深海夜未眠 2024-10-26 22:55:23

如果您的“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文