Java: Passing the same objects to each others constructor

前端 未结 6 2269
粉色の甜心
粉色の甜心 2021-01-21 10:56

Evening everyone.

I seem to have hit an odd problem when trying to pass an object to another objects constructor who\'s constructor also relies on the object it\'s being

相关标签:
6条回答
  • 2021-01-21 10:56

    I've have objects reference each-other before. I.e. in a game an object might contain a reference to it's cell, and the cell can contain a reference to the object.

    You just have to be careful regarding how you create your objects.

    Nothing says that they have to both be passed into each-others constructors, in-fact you can create both objects, and then in a different method, have them reference each-other.

    It's also not an uncommon practice to check if a value is null before using it(but only do this is the value actually has an excuse to ever be null.)

    0 讨论(0)
  • 2021-01-21 11:05

    This seems like a nice example for a vicious circle.

    I would suggest breaking the circle by using a separate method, e.g. addToolbar(), to add the other object to one of the classes, rather than a constructor. You would only need to add a few checks to avoid illegal object states, if that method has not been called:

    class WebPanel {
        public void addToolbar(Toolbar toolbar) {
            ...
        }
    }
    ...
    class ToolBar {
         public ToolBar(WebPanel parent) {
             ...
         }
    }
    ...
    WebPanel webpanel = new WebPanel();
    webpanel.addToolbar(new Toolbar(webpanel));
    

    In general, it is preferred for "child" objects to take the parent object as a constructor argument and then be added to the parent via a method call, rather than the other way around. In a UI the outer elements are parents to their contents - in your case I would probably expect a WebPanel to contain - and thus be a parent of - a ToolBar object...

    0 讨论(0)
  • 2021-01-21 11:05

    Mmn.. what you could do is create a model object that wraps around your toolbar and WebPanel.

    public class SomeModel{
      WebPanel panel;
      Toolbar toolbar;
    }
    

    Or you could create your toolbar object.. and in the constructor of the toolbar create your webpanel

    public WebPanel()
    {
       this.toolbar= new Toolbar(this); 
    }
    webPanel = new WebPanel();
    webPanel.getToolbar() ;
    

    Ok that was cheating lmao It depends whether one is a composite object of another; although I think the Model way is better.. no circular reference.

    0 讨论(0)
  • 2021-01-21 11:07

    This will lead to problems as you can see. Instead of this approach, you can use the setter-getter method approach where you construct the object with a default constructor such as

    ToolBar myToolBar = new ToolBar();
    WebPanel webPanel = new WebPanel();
    

    and then use the setter methods to set the required instance variables which are required for the object to be fully constructed.

    myToolBar.setWebPanel(webPanel);
    webPanel.setToolBar(myToolBar);
    
    0 讨论(0)
  • 2021-01-21 11:20

    Maybe if you declare first

    WebPanel webPanel = new WebPanel(myToolBar);
    

    and then

    ToolBar myToolBar = new ToolBar(webPanel);
    

    The object must exist first to be passed.

    0 讨论(0)
  • 2021-01-21 11:22

    There are multiple ways to do it; I usually prefer to pass parameter to one of the two and have it call a setter method in the other:

    class ToolBar {
      void setWebPanel(WebPanel wp) { 
        _wp = wp; 
      }
      ....
    }
    
    class WebPanel {
      WebPanel(ToolBar t) {
        _t = t;
        _t.setWebPanel(this);
      }
    }
    
    0 讨论(0)
提交回复
热议问题