Another MVC
thread hurrah! I can however assure you all that I've tried to read up on MVC as much as possible including a fair number of threads on these here forums. I *think* I've finally gotten my head around it to the extent that I was able to produce some code, but I'm hoping someone can point out the, inevitable, mistakes I've made as I lack experience with
Java and with
Patterns.
So a brief breakdown of MVC as I understand it (I should say I'm talking about a desktop type application).
Model - Contains the data for the application and any tightly coupled functionality for said data.
View - Displays data, takes user input.
Controller - processes events from View, updates model, updates view
I've seen a lot of examples that make the View an Observer of an Observable Model (usually using Java's built-n Observer/Observable interface/class), I'm guessing that this wouldn't strictly be MVC as it allows the View direct access to the Model?
In my code I decided to go with that approach because it was making sense to me at the time (sorry if its wrong!). I ended up writing my own Observer - Observable, partly to see if I grasped it and partly for fun. I won't bother posting the code for the Listener or Subject interfaces unless anyone really wants/needs me too.
The View is a JFrame with a few buttons/textfields and labels. Whatever you enter into textBox1 will end up displayed in label1, whatever you enter into textbox2 will be displayed in label2. The Model just contains two Strings and a little code for the listening system. There are two controllers that provide slightly different functionality one clears the textBox it gets the data from, the other doesn't.
Forwards to code.
View
Model
Controller1
Controller2
Pretty much the same as Controller1 but it clears the textBox.
And the Application code if anyone is really interested
It *works*, ie the behaviour is what I would expect. The code posted compiles/runs (provided you create the Listener and Subject interfaces).
Is this MVC, is it even close?
Any thoughts would be very much welcome!