Programming and Problem Solving with Java

(やまだぃちぅ) #1
11.2 List Class | 525

Figure 11.2 List with Duplicate Keys and List with Unique Keys


11.2 List Class


In this section, we will design and implement a general-purpose class that rep-
resents a list of items. Let’s think in terms of a to-do list. Before we begin to brain-
storm, however, we must ask an important question: For whom are we designing
the class? We may be designing it for ourselves to keep in our library of classes.
We may be designing it for others to use in a team project. When we create a
class, the software that uses it is called the clientof the class. In our discussion,
we will use the terms clientand userinterchangeably, as we sometimes think of
them as referring to the people writing the software that uses the class, rather than
the software itself.


Brainstorming the List Class


Because we are designing a general-purpose class, our brainstorming must be more specu-
lative. We don’t have a specific problem to solve; we have to think in terms of what we cur-
rently do with our to-do lists as well as what other things we might like to do if we could.
Ideally, we will start with an empty list each morning and add things to it. As we accomplish
a task on the list, we will cross it off. We will check whether an item is already on the list. We
will check whether we can add one more item to the list. We will check whether the list is
empty (we wish!). We will go through the list one item at a time.
Let’s translate these observations into responsibilities for the list, in the form of a CRC
card. Notice that constructors are obvious because they are prefaced by “create,” and ob-
servers have a “returns” shown. Likewise, the choice of identifier for transformers gives their
type away: insertand delete. Note that this is our first experience with an iterator.


Client Software that declares
and manipulates objects of a
particular class
Free download pdf