views:

52

answers:

2

I have a simple java gui calculator, with 3 number systems (there are some bugs but that doesn't matter now). Currently all code is in one file. My task is to rewrite it as MVC, and add possibility to run it in either gui or console mode. How should I divide this program to organise it as M-V-C ? Is it written properly enough to add console functionality to it? (guess I'll have to change all methods invoking to JLabel Output to something simply storing an output String as a model argument and then having View to get it).

Here's the starting code :

http://paste.pocoo.org/show/224566/

Here's what I already have :

Main :
http://paste.pocoo.org/show/224567/
Model :
http://paste.pocoo.org/show/224570/
View :
http://paste.pocoo.org/show/224569/
Controller :
http://paste.pocoo.org/show/224568/

I don't have view in my model so I can't call to Output. That's the first problem I can see.

A: 

The current separation looks good. Here are some pointers for implementing the console view:

  • expose the controller's actions in a UI independent way. Such as an abstract Action class. The view then invokes the action in response to UI gestures. This keeps the controller independent of the view implementation, and allows the same controller to be used by multiple views.
    • add notifications from the model of changes, to keep views in sync.

The Console View can then read standard input, and write status to standard output by querying the model, and invoke functions using the Actions exposed by the controller.

A good test of MVC is creating two views on the same model and controller - both should work correctly and update from changes from the other.

mdma
A: 

You should get familiar with the Observer pattern. This pattern will allow your model to change any time, but without the need to know the different views (which is what we are looking for).

Simply put, the model will say: "Hey, I've changed. Anyone who is interested should act accordingly".

Federico Cristina