Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^288) | Object-Oriented Software Design and Implementation
sophisticated steps. Functional decomposition provides a way to break such a
problem down into simpler pieces that are easier to solve.


Pseudocode You’ll find it easier to implement a design if you write the steps in


pseudocode.Pseudocodeis a mixture of English statements and Java-like control
structures that can be translated readily into Java. (We’ve been using pseudocode
in the algorithms in the Problem-Solving Case Studies.) When a concrete step is
written in pseudocode, it should be possible to rewrite it directly as a Java state-
ment in a program.
Always remember that the problem-solving phase of the programming process
takes time. If you spend the bulk of your time analyzing and designing a solution,
coding (implementing) the program should take relatively little time.

Constructor An operation that
creates a new instance of a class


Transformer An operation
that changes the internal state
of an object


Observer An operation that
allows us to observe the state of
an object without changing it


Copy constructor An opera-
tion that creates a new instance
of a class by copying an existing
instance, possibly altering some
or all of its state in the process


Iterator An operation that al-
lows us to process—one at a
time—all the components in an
object


Categories of Responsibilities


Class instance responsibilities generally fall into three categories:constructors,transformers, and ob-
servers.
A constructor is an operation that creates a new instance of a class. Operators that modify
the state of an object are transformers. For example, an operation that changes the year of a Date
object is a transformer. The knowFirstNamemethod of a Nameclass is an example of an observer.
Some operations are combinations of observers and constructors. An operation that takes a
Dateobject and an integer value and returns a new Dateobject that is the original date plus that
number of days is an example of an observer (of the original Dateobject) and a constructor (of
the new Dateobject). This particular case is an example of a copy constructor.
In addition to the three basic categories of responsibility, there is a fourth category that is less
common: iterators.
In later chapters we will examine classes that are made up of multiple values, all of the same
type. An iterator allows us to observe each of these components one at a time. For example,
given a class representing a list of names, an iterator would enable us to go through the list, ob-
serving or transforming each of the names one by one.
Free download pdf