Java Reference
In-Depth Information
Once we try to make modifications to the project, however, we will notice significant
differences in the amount of work involved in changing badly designed code, compared with
changes to a well-designed application. We will investigate this by making some changes to the
project. While we are doing this, we will discuss examples of bad design when we see them in
the existing source, and we will improve the class design before we implement our extensions.
6.5.1 The task
The first task we will attempt is to add a new direction of movement. Currently, a player can
move in four directions: north , east , south , and west. We want to allow for multilevel buildings
(or cellars, or dungeons, or whatever you later want to add to your game) and add up and down
as possible directions. A player can then type "go down" to move, say, down into a cellar.
6.5.2
Finding the relevant source code
Inspection of the given classes shows us that at least two classes are involved in this change:
Room and Game .
Room is the class that stores (among other things) the exits of each room, and, as we saw in
Code 6.1, in the Game class the exit information from the current room is used to print out infor-
mation about exits and to move from one room to another.
The Room class is fairly short. Its source code is shown in Code 6.3. Reading the source, we can
see that the exits are mentioned in two different places: they are listed as fields at the top of the
class, and they get assigned in the setExits method. To add two new directions, we would
need to add two new exits ( upExit and downExit ) in these two places.
Code 6.3
Source code of the
(badly designed) Room
class
public class Room
{
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
/**
* Create a room described "description "Initially, it
* has no exits. "description" is something like
* "a kitchen" or "an open courtyard".
*/
public Room(String description)
{
this .description = description;
}
Search WWH ::




Custom Search