Hi guys. I have a question, because this is coming up over at the
SCJD. My knowledge of the MVC pattern over 7 years has always been that one class needs to know about the others in order to completely decouple the classes so that you can substitue any Controller, Model, or View.
The controller is that class. hence it's name controller. I Know that there are indirection ways of decoupling further, however it adds extra classes that really don't necessarily help make things easier to swap out and decouple.
Here's my concept.
1. The GUI should know only one thing, how to display. It has Hook methods to hook in any kind of ActionListener it wants. Now this GUI is completely decoupled and encapsulated from any other class period.
2. The Model is the Model and that's all. I am sure we have no argument on this.
3. the controller, the workhorse of it all. It controls the interaction between all three classes. So therefore it needs to know about the GUI and the Models. Using anonymous inner classes "ActionListeners" I can hook my methods in the controller to the actions of the GUI. There is a seperate method in the controller for each action I want to handle. I can chose to only have the controller hook into a couple of the actions of the GUI and not others. I could have a second controller handle the other actions. I can swap out another controller to handle the actions differently, all without changing the GUI. controller is not an ActionListener itself.
Now here is where I think is what you guys are talking about, you can move these anonymous inner class ActionListeners to their own class, that passes the call to the controller, and yes now the Controller doesn't need to have any reference to the GUI. But is this 100% necessary. I mean I can change the GUI that the controller uses, but I could have all the GUI's implement an interface and the Controller has a reference type to the interface, and therefore any GUI of that type will work. Because really thinking about it the methods in the controller are for a specific type of GUI. Meaning a controller for a Purchasing System deals with PO's and won't work for an Adress Book application.
So in all these cases I can swap out any of the three classes without causing any affect to the other two.
Comments.
Mark