Java Reference
In-Depth Information
All that is missing, then, is the showCommands method in the Parser , which delegates this
task to the CommandWords class. Here is the complete method (in class Parser ):
* Print out a list of valid command words.
public void showCommands()
Exercise 6.16 Implement the improved version of printing out the command words, as
described in this section.
Exercise 6.17 If you now add another new command, do you still need to change the Game
class? Why?
The full implementation of all changes discussed in this chapter so far is available in your code
examples in a project named zuul-better. If you have done the exercises so far, you can ignore
this project and continue to use your own. If you have not done the exercises but want to do
the following exercises in this chapter as a programming project, you can use the zuul-better
project as your starting point.
Thinking ahead
The design we have now is an important improvement to the original version. It is, however,
possible to improve it even more.
One characteristic of a good software designer is the ability to think ahead. What might change?
What can we safely assume will stay unchanged for the life of the program?
One assumption that we have hard-coded into most of our classes is that this game will run as a
text-based game with terminal input and output. But will it always be like this?
It might be an interesting extension later to add a graphical user interface with menus, but-
tons, and images. In that case, we would not want to print the information to the text terminal
anymore. We might still have command words, and we might still want to show them when a
player enters a help command. But we might then show them in a text field in a window, rather
than using System.out.println .
It is good design to try to encapsulate all information about the user interface in a single
class or a clearly defined set of classes. Our solution from Section 6.9, for example—the
showAll method in the CommandWords class—does not follow this design rule. It would
be nicer to define that CommandWords is responsible for producing (but not printing !)
the list of command words, but that the Game class should decide how it is presented to
the user.
Search WWH ::

Custom Search