Creating a static HashMap and setting it into a class in Java--will it be truly static?
I'm writing a line by line interpreter for class and I'm using Java to do it. It needs to be dynamically scoped, which has proved to be mentally harder to implement than lexical scoping for me. (I missed this requirement when I began.)
In my solution I have classes to model functions; but since binding has to be global I was thinking that I could create a class whose only job was to contain a static HashMap for all my variable bindings, but set each function class's HashMap to point to it.
What I'm not sure about is if this will work as intended; if I have multiple functions, can I guarantee that they all have acce开发者_运维百科ss to the data in my binding HashMap? If the answer to this is yes, than I have my solution in hand. Otherwise, back to the drawing board...
I would strongly suggest not to use static data structures in this case. Imagine if you wanted to support two parallel sessions in a single run of your program. They would interfere with each other, wouldn't they. The same troubles would happen if you needed to provide tests to your code base.
My suggestion is to contain an instance reference to the Hashmap. As Robin suggests you may put it into some common superclass so that duplication could be avoided. But still each object representing a function would need the reference to the Hashmap to be put as a configuration.
This all makes me think you might be inspired by the idea of Dependency Injection.
精彩评论