N
Novice
I am trying to make sure I have the right idea in designing a class for a
game I am writing.
The game is capable of showing the score either during the game or at the
end. In both cases, I simply display a dialog showing the score. If the
game isn't over yet, I entitle the dialog "Interim Score". If the game is
finished, I entitle the dialog "Final Score". When the player is given the
interim score, the button at the bottom says "Quit" and he is returned to
the game when he presses it. When the player is given the final score, the
button at the bottom says "Exit" and the game itself ends when he presses
it.
At the moment, I have a single class that gets invoked to show both the
interim score and the final score. That class is passed a boolean which
indicates if it is an interim score or a final one. Based on that boolean,
the title of the dialog is set appropriately and the button text is set to
either Quit or Exit. The actionPerformed() doesn't change: Quit causes a
dispose() and Exit causes a System.exit(0).
While the dialog is simple and works just fine, I suspect I'm violating
some OO principles by making the behavior of the class change depending on
what kind of score is being shown.
Am I right in thinking that I should actually have an abstract class, maybe
called AbstractScore, and that it should have two subclasses, Interim Score
and FinalScore? AbstractScore could have all of the "common code" in it
while InterimScore and FinalScore could each do the stuff that was unique
to itself, like setting the Title appropriately, setting the text of the
button, and reacting to the appropriate button in actionPerformed(). In
other words, the actionPerformed() in InterimScore would only react to Quit
while the actionPerformed() in FinalScore would only react to Exit. Then,
my game should instantiate InterimScore during the game and FinalScore when
the user wants to end the game.
Or is there a better way?
game I am writing.
The game is capable of showing the score either during the game or at the
end. In both cases, I simply display a dialog showing the score. If the
game isn't over yet, I entitle the dialog "Interim Score". If the game is
finished, I entitle the dialog "Final Score". When the player is given the
interim score, the button at the bottom says "Quit" and he is returned to
the game when he presses it. When the player is given the final score, the
button at the bottom says "Exit" and the game itself ends when he presses
it.
At the moment, I have a single class that gets invoked to show both the
interim score and the final score. That class is passed a boolean which
indicates if it is an interim score or a final one. Based on that boolean,
the title of the dialog is set appropriately and the button text is set to
either Quit or Exit. The actionPerformed() doesn't change: Quit causes a
dispose() and Exit causes a System.exit(0).
While the dialog is simple and works just fine, I suspect I'm violating
some OO principles by making the behavior of the class change depending on
what kind of score is being shown.
Am I right in thinking that I should actually have an abstract class, maybe
called AbstractScore, and that it should have two subclasses, Interim Score
and FinalScore? AbstractScore could have all of the "common code" in it
while InterimScore and FinalScore could each do the stuff that was unique
to itself, like setting the Title appropriately, setting the text of the
button, and reacting to the appropriate button in actionPerformed(). In
other words, the actionPerformed() in InterimScore would only react to Quit
while the actionPerformed() in FinalScore would only react to Exit. Then,
my game should instantiate InterimScore during the game and FinalScore when
the user wants to end the game.
Or is there a better way?