MVC - circular dependency

circular-dependency, java, model-view-controller

Solution

One possible solution:

- Simply give Controller an `addView(View view)` method

- Likewise for View give it an `addController(Controller controller)` method.

- Make it part of your documentation that these must be set before use.

- Make sure you check that the reference variables are not null before using them, since they won't be set in the constructor.

Problem

I need the view to hold a reference to the contoller because it needs to register the controller as an event listener. I need the contoller to hold a reference to the view, because upon button click, I need to be able to get the selected files in a list. (I have a list of files, and a button 'Add cluster', so when the button is clicked I need to get the selected files) So in short I have: ``` Controller controller(view); View view(controller); ``` I'm sure there's some bad design here, I just can't figure out how to avoid it..

Original source

Related problems