Original Post
Hi everyone! I'm trying to grasp the concept of Model-View-Controller. I would say that theoretically I know how everything should work. But when it comes to implementing the stuff, I'm little confused... it seems to me like the concept breaks. For example, lets say I have an application that has a Phonebook displayed on screen and a button to add new data to the phonebook. Ok, so I have the classes like this: The PhonebookControlleris initialized with both a Phonebook and a PhonebookGUI: But as I see it the PhoneBookController is now BOUND to the specific VIEW. So now for instance, I could not change the PhonebookGUI from a visual gui with the "addButton" to a console view which uses the input from console as actions. So, it's kind of pointless separating the View and controller in this case?? What I can think of instead, would be to have the controller implement an interface like this: Now I could have the PhonebookController implement this PhonebookGUIListener interface, and register itself by the PhonebookGUI. So, I could now implement a Console version of PhonebookGUI and be asured that the application would still work correctly even though I switched the interface. But on the other hand, the PhonebookController is no longer a controller in the same sence as the Model-View-Controller intended it to be?? Also, now the PhonebookGUI will need to handle some of the GUI events on its own, which is initially intended to be handled by the Controller? Any comments on this would be appreciated! Now my question is the following. How would I handle the events in the PhonebookController, whithout having it BOUND to a specific VIEW?? For example, I would want to be able to switch from a Windowed view to a console view... whithout having to replace the entire PhonebookController as well. Thx !
//This is the model class
class Phonebook {...}
//This is the view
class PhonebookGUI {...}
//This is the controller
class PhonebookController{...}
PhoneBookController myDefaultController = new PhoneBookController(phonebook, phonebookGui);
Then the PhoneBookController registers itself at the PhonebookGUI to catch all events, and then when an event from the PhonebookGUI arrives it is handled like this:
PhonebookController::actionPerformed(Event e)
{
//Now we need to determine what even was fired:
if(e.getSource() == phoneBookView.addButton)
{
//We now control what should happen when the add button is pressed
}
}
interface PhonebookGUIListener
{
public void onGUIAddPhonebookEntryEvent(...);
}