views:

63

answers:

1

Hello,

My application is following the MVC design pattern. The problem I keep running into is needing to call methods inside a Controller class from outside that Controller class (ex. A View class wants to call a Controller method, or a Manager class wants to call a Controller method). Is calling Controller methods in this way allowed in MVC? If it's allowed, what's the proper way to do it?

According to the version of MVC that I am following (there seems to be so many different versions out there), the View knows of the Model, and the Controller knows of the View. Doing it this way, I can't access the controller. Here's the best site I've found and the one describing the version of MVC I'm following: http://leepoint.net/notes-java/GUI/structure/40mvc.html. The Main Program code block really shows how this works.

Thanks for any answers.

+1  A: 

Take a closer look at this paragraph from the article you linked to:

View This View doesn't know about the Controller, except that it provides methods for registering a Controller's listeners. Other organizations are possible (eg, the Controller's listeners are non-private variables that can be referenced by the View, the View calls the Controller to get listeners, the View calls methods in the Controller to process actions, ...).

You have the observer pattern here between the View and the Controller. MVC is not a single pattern per se but at least two combined.

One way to get your head around managing the View/Controller communication is to use events. The View fires events on certain user actions (without knowing necessarily who might handle them.) The Controller processes these events and acts accordingly.

Paul Sasik
Yes, I am using events in the way you are saying, but what I am most interesting in is "the View calls methods in the Controller to process actions." The article says that this is possible in "other organizations" of MVC. But how? I do not have access to controllers in the view (or anywhere else).
JT703
Events can be used to process actions as well. But, to keep things more synchronous but still decoupled you could define an interface that the controller would implement. The interface would then be passed to the view at some initialization point. The view would then call the Controller indirectly via that interface.
Paul Sasik