I've been programming in Java for about 6 months, and I thought a good and challenging test of my skills would be to build a GUI calculator. I have very recently heard about this MVC idea, and I thought (perhaps foolishly) that it would be a good fit for this pet project. But I'm running into trouble during the design, and I'm not sure how to proceed.
So I have a Model
, View
, and Controller
class. The Model
is responsible for all the actual calculation; the View
draws the calculator and the display; and the Controller
:
- passes button clicks to
View
- gets the display value from
Model
and passes it toView
Ah, but the buttons! I want to have a Button
interface, where a Button
instance has:
- (unique) ID number;
- Name;
- function.
Then I would have my Controller
build up a collection of Button
s, and [not sure how to do this] let both the View
and Model
use those buttons: View
for drawing, Model
for calculating. Thus I have the revised list for Controller
:
- pass clicks to
View
- return
Button
ID toModel
- get display value from
Model
and pass it toView
But the more I work on this, the more I can't figure out how this would all come together. So I have several related questions:
- Should
View
really be responsible for sorting out whichButton
a click corresponds to? Should that be done inController
? - Should I have more than one type of
Button
? Is it necessary to distinguish between numbers, operators, equals, etc.? Which of the three MVC classes needs to know the difference? - Is MVC even the right way to go here? Is there a more fundamental, accepted way of building an OOP calculator?
- If MVC is indeed appropriate, what am I doing wrong? I think adding in the
Button
s has really thrown me off.