01 Design Issues for Object- Oriented Languages
01 Design Issues for Object- Oriented Languages
11.2.3 An Example
A stack is a widely applicable data structure that stores some number of data
elements and only allows access to the data element at one of its ends, the top.
Suppose an abstract data type is to be constructed for a stack that has the fol-
lowing abstract operations:
Note that some implementations of abstract data types do not require the
create and destroy operations. For example, simply defining a variable to be of
an abstract data type may implicitly create the underlying data structure and
initialize it. The storage for such a variable may be implicitly deallocated at the
end of the variable’s scope.
A client of the stack type could have a code sequence such as the following:
. . .
create(stk1);
push(stk1, color1);
push(stk1, color2);
temp = top(stk1);
. . .
types. Among these are assignment and comparisons for equality and inequality.
If the language does not allow users to overload assignment, the assignment
operation must be included in the abstraction. Comparisons for equality and
inequality should be predefined in the abstraction in some cases but not in
others. For example, if the type is implemented as a pointer, equality may mean
pointer equality, but the designer may want it to mean equality of the structures
referenced by the pointers.
Some operations are required by many abstract data types, but because
they are not universal, they often must be provided by the designer of the type.
Among these are iterators, accessors, constructors, and destructors. Iterators
were discussed in Chapter 8. Accessors provide a form of access to data that is
hidden from direct access by clients. Constructors are used to initialize parts of
newly created objects. Destructors are often used to reclaim heap storage that
may be used by parts of abstract data type objects in languages that do not do
implicit storage reclamation.
As stated earlier, the enclosure for an abstract data type defines a single
data type and its operations. Many contemporary languages, including C++,
Objective-C, Java, and C#, directly support abstract data types.
The first design issue is whether abstract data types can be parameterized.
For example, if the language supports parameterized abstract data types, one
could design an abstract data type for some structure that could store elements
of any type. Parameterized abstract data types are discussed in Section 11.5.
The second design issue is what access controls are provided and how such
controls are specified. Finally, the language designer must decide whether the
specification of the type is physically separate from its implementation (or
whether that is a developer choice).