Java Reference
In-Depth Information
Self-Test Exercises (continued)
3. Suppose aStudent is an object of the class Student . Based on the inheritance dia-
gram in Display 12.3, where will you find the definition of the method used in the
following invocation? Explain your answer.
aStudent.setNumber(4242);
12.2
Patterns
I bid him look into the lives of men as though into a mirror, and from
others to take an example for himself.
TERENCE (Publius Terentius Afer) 190-159 B.C.,
Adelphoe
Patterns
are design outlines that apply across a variety of software applications. To be
useful, the pattern must apply across a variety of situations. To be substantive, the pat-
tern must make some assumptions about the domain of applications to which it
applies. For example, one well-known pattern is the
pattern
Container-Iterator
pattern. A
Container-
Iterator
container
container
is a class (or other construct) whose objects hold multiple pieces of data.
One example of a container is an array. Other examples, which will be discussed later
in this topic, are vectors and linked lists. Any class or other construct designed to hold
multiple values can be viewed as a container. For example, a
value can be
viewed as a container that contains the characters in the string. Any construct that
allows you to cycle through all the items in a container is an
String
. For example, an
array index is an iterator for an array. It can cycle through the array as follows:
iterator
iterator
for ( int i; i < a.length; i++)
Do something with
a[i]
The index variable
is the iterator. The Container-Iterator pattern describes how an
iterator is used on a container.
In this brief chapter we can give you only a taste of what patterns are all about. In this
section we will discuss a few sample patterns to let you see what patterns look like. There
are many more known and used patterns and many more yet to be explicated. This is a
new and still developing field of software engineering.
i
Adaptor Pattern
The
Adaptor pattern transforms one class into a different class without changing
the underlying class but merely by adding a new interface. (The new interface
replaces the old interface of the underlying class.) For example, in Chapter 11 we
mentioned the stack data structure, which is used to, among other things, keep
track of recursion. One way to create a stack data structure is to start with an array
Adaptor
Search WWH ::




Custom Search