November 15th, 2012, 05:50 AM
What is good practice for the relationship between view and controller in the MVC pattern?
It appears to me that this relationship can become quite strongly coupled and is this desirable?
To discuss this issue here is a very generic example. Please ignore typos, which platform, graphical library to use and lack of using smart points etc.
Say you have a view element that contains a textbox, a button with caption "add" and a listbox. So the user could type in "ABC" in the textbox and click "add" button and the string "ABC" would appear in the listbox.
Here is a simple class definition for each of the classes;
The view class should subscribe to changes in the model class using the observer pattern.
class Model // observable
void add(const std::string& s);
const std::vector<std::string>& getList();
In my opinion it makes sense for the controller to create and own an instance of the view class. So that would mean something like this;
An interesting description of a controller found online stated:
So when a user clicks on the "add" button located in the view this action must be forwarded to the controller instance which will update the model. This can be handled at least two ways,
The controllers are typically responsible for calling methods on the model that change the state of the model.
a) The View contains a reference to the Controller which has a "add" method.
b) The View is observable and the controller subscribes to its changes.
This might not be a big deal but it appears that for each "operation" (i.e. button click) in the view, there might be a need for a matching method in the controller (maybe exaggeration a bit). So you might have multiple views with different operations but the controller must have a method for each operation. Can the controller class become bloated? Is there a generic way of solving this issue by reducing the coupling between View and Controller?
, make an effort yourself, try googling
or search here
, give a proper description of your problem, include code* and error messages.
*All code should include code tags
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!