How to populate list of object in class A by class B constructor

1.6k views Asked by At

I have an issue: There are 2 classes that implements some interfaces. In class A I have a list of objects of class B.

Is there exist a way to populate these list using class B constructor instead of hardcode every object that will be created? In class A I have method that search for particular class B object by given parameter, but the problem is to figure out how to "autopopulate" this list every time the new object is created.

I Was trying to invoke method addNodeToList() form class A, but witout an instance of this class I cant do that.

Is there any elegant way to autopopulate this list?

here is the 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: I was thinking about create static list inside of class B, but I think it is not a good practice. I am out of idea how to solve this the efficent way.

FINISHED: I have created a method Inside of class A like this:

public class MyStructure implements IMyStructure {
public void addNodeToList(String code, String renderer) {

        CompositeNode node = new CompositeNode(code, renderer);

        nodes.add(node);
    }

It works the way I wanted - I dont store the list inside of class B and it populate the list it self.

2

There are 2 answers

2
Grzegorz Górkiewicz On BEST ANSWER

Without knowing further context and usage I would simply add a getter in class A:

public class MyStructure implements IMyStructure {
    //...
    public List<INode> getNodes() {
        return this.nodes;
    }
   //..
}

and then it can be used in class B to add an element to the list of class A:

public CompositeNode(String code, String renderer){
    //...
    myStructure.getNodes.add(this);
    //...
}

I don't think you need the addNodeToList method, unless used elsewhere.

3
user8589247 On

You can instantiate the list of INode objects and add the first node in the MyStructure's constructor so you can create CompositeNode with MyStructure object filled with CompositeNode object in this way:

MyStructure

public class MyStructure implements IMyStructure {
    private List<INode> nodes;

    public MyStructure() {
        super();
    }

    public MyStructure(INode node) {
        nodes = new ArrayList<>();
        nodes.add(node);
    }
    //...
}

CompositeNode

public class CompositeNode implements ICompositeNode {
    private String code;
    private String renderer;
    private MyStructure myStructure;

    public static int counter;

    public CompositeNode(String code, String renderer){
        this.code= code;
        this.renderer=renderer;
        myStructure = new MyStructure(this);
        counter++;
    }
    //...
}