Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: give access for created objects to a shared resource
    primarykey
    data
    text
    <p>The situation before:</p> <p>A Resource exists. Class Root is created, and during it's creation it creates Child classes (many implementations of one base). Those can, but don't have to, posses children. Basically, this is a typical tree structure, that builds itself.</p> <p>One of the Child classes "NeedyChild" requires access to a Resource that was opened during creation of Root. There was only one Resource possible, so I made it a singleton and accesed it from NeedyChild by Resource.getInstance();</p> <p>The situation now:</p> <p>I have to build many trees. Those are independent of each other and take quite some time, so I made it concurrent. Unfortunatelly, each tree has it's own Resource.</p> <p>Question: how to access the Resource specific to the current runnable now?</p> <p>My solutions:</p> <ol> <li><p>Make Child base class require Resource parameter in it's constructor. Pros: it's quick. It's fairly efficient (passing a reference is no big deal), it's simple. Cons: it requires me to change constructor calls and signatures throughout the application, possibly >100 places.</p></li> <li><p>Reflection to make base constructor find it's caller, save reference, then just give Resource to Root, and NeedyChild will go up "creators" hierarchy to get Root and then Resource. But I won't do that, it's evil.</p></li> <li><p>Didn't research it, but maybe I can access the parent Runnable the code is currently executing inside? Cast it back to my RunnableClass which could hold reference to Resource? The problems are: I don't know if it's possible, it seems a bit "wrong" too...</p></li> <li><p>(not really working) Make the tree traversable both ways, give Resource to Root and from NeedyChild getParent() long enough until you're at Root. It would be great, but the NeedyChild needs Resource during it's creation, and only after it is created, it is appended to the parent. I thought of delaying filling Resource-dependent fields, but it gets ugly very quick.</p></li> </ol> <p>TBH I probably will have to modify the base constructor and either pass "this" (so I can traverse back to the Root), or pass Resource. The runnable-specific context seems also quite decent, but I'm already doing the parallel procesing to make things faster, I don't really want to negate it by some very slow reflection lookups...</p> <p>So, any ideas?</p> <p>Addendum 1: Sample Code</p> <pre><code>abstract class BaseElement{ List&lt;BaseElement&gt; children = new ArrayList&lt;&gt;(); public void addChild(BaseElement child){ children.add(child); } } class Loader{ private PackageElement resultPackage; private Resource resource; public Loader(Resource resource){ this.resource = resource; } public callMeFromOutside(SourceTree source){ resultPackage = new Package(source.pack()); } } class PackageElement extends BaseElement { public PackageElement(PackageTree pack){ this.addChild(new Element(pack.element())); } } class Element extends BaseElement{ public Element(ElementTree element){ this.addChild(new NeedyChild(element.needyChild())); } } class NeedyChild extends BaseElement{ public NeedyChild(NeedyTree needy){ this.setSomethingImportant(resource.loadSomethingBasedOn(needy)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload