11

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..

Shmoopy
  • 5,334
  • 4
  • 36
  • 72
  • Do as [Swing did](http://www.oracle.com/technetwork/java/architecture-142923.html) and combine the Controller and the View. – Jeffrey Jun 02 '12 at 19:30
  • I would argue against doing this in MVC since from what I recall it's not really made for server side event handling. Perhaps handling events with jQuery would fit better here. – Lilienthal Jun 02 '12 at 19:56

5 Answers5

6

I'm not sure what Java Technologies you're using, but in GWT applications -and using MVP pattern- there's no need of the View to have a reference to the Controller: All the communication between the Controller (or Presenter) and the View is made through an interface implemented by the View. In your particular case, your code should look like this:

Define a Display inteface:

public interface Display {
    public void registerEventListener(Listener aListener)
    public List getSelectedFiles ()
}

Let the View implement that interface:

public class View implements Display{
//The method implementations
}

And make all the necessary bindings in the controller:

public class Controller{
    private Display view;
    public Controller(){
        //Or use some DI technology
        this.view = new View();
        //Get a Listener implementation, maybe an Anonymous Inner Class
        this.view.registerEventListener(getListener());
    }

    public void processFiles(){
        List files = view.getSelectedFiles();
        //Do the processing here
    }

}
Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
4

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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I'm pretty sure View shouldn't depend on Controller on MVC pattern: It must only depend on model – Carlos Gavidia-Calderon Jun 02 '12 at 20:27
  • You're suggesting that the View should have a reference to the Controller: That's a dependency – Carlos Gavidia-Calderon Jun 02 '12 at 20:31
  • 3
    @Carlos: To clarify, MVC is not a strict definition at all, but a general paradigm for separating out concerns. There are MVC structures where the view does in fact need a reference to the control. I think that the most important feature is that the model mustn't care at all about the view. For example, check out these diagrams and the discussion: [MVC Diagrams](http://stackoverflow.com/questions/5966905/which-mvc-diagram-is-correct-web-app). – Hovercraft Full Of Eels Jun 02 '12 at 20:42
1

Well, I believe you can use encapsulation and also the lazy initialization process. I am sure you do not need the View at the same moment when the controller is getting initialized or vice-versa. If not you can also the above answer in conjunction to the lazy init property for beans.

dharam
  • 7,882
  • 15
  • 65
  • 93
  • 1
    I'm not the original poster, but can you show a brief example of just what you mean? Thanks! – Hovercraft Full Of Eels Jun 02 '12 at 20:17
  • Well what I wanted to suggest him was, if the view is needed after the controller is injected then he can configure the view as follows: This will not require the view to be initialized before the controller. – dharam Jun 02 '12 at 20:21
0

To bind view and controller properly right click on *.aspx page and select "go to controller" menu. Now in the respected controller add a method with the following syntax.

public ActionResult viewName()
{
   return view()
}  

where viewname is the name of your view for ex. in .net it is name of page without aspx extension Now when you will right click on view and will select menu "Go to view" which will move you to respected page this confirms that you r view and controller has been binded well.

Prajwal
  • 71
  • 1
  • 2
0

Create Four Sections : 1-Control 2-Model 3-View 4-ViewInterface

Section 4 is middle layer between Control and View. When you initialize View initialize IControl interface like

IControl control = new ControlUtil((IViewUtil(new ViewUtil());

You pass view interface to control to be used. When view want calls control, it is like saying "Hey control do something and here is the my interface in case of you need it.