java - How to populate list of object in class A by class B constructor -
i have issue: there 2 classes implements interfaces. in class have list of objects of class b.
is there exist way populate these list using class b constructor instead of hardcode every object created? in class have method search particular class b object given parameter, problem figure out how "autopopulate" list every time new object created.
i trying invoke method addnodetolist() form class a, witout instance of class cant that.
is there elegant way autopopulate list?
here code:
class a
public class mystructure implements imystructure { private list<inode> nodes; public mystructure() { } @override public inode findbycode(string code) { //impl } @override public inode findbyrenderer(string renderer) { //impl } @override public int count() {{ //impl } public void addnodetolist(inode node){ nodes.add(node); } }
class b
public class compositenode implements icompositenode { private string code; private string renderer; mystructure mystructure; public static int counter; public compositenode(string code, string renderer){ this.code= code; this.renderer=renderer; mystructure.addnodetolist(this); counter++; }
edit: thinking create static list inside of class b, think not practice. out of idea how solve efficent way.
finished: have created method inside of class this:
public class mystructure implements imystructure { public void addnodetolist(string code, string renderer) { compositenode node = new compositenode(code, renderer); nodes.add(node); }
it works way wanted - dont store list inside of class b , populate list self.
without knowing further context , usage add getter in class a:
public class mystructure implements imystructure { //... public list<inode> getnodes() { return this.nodes; } //.. }
and can used in class b add element list of class a:
public compositenode(string code, string renderer){ //... mystructure.getnodes.add(this); //... }
i don't think need addnodetolist
method, unless used elsewhere.
Comments
Post a Comment