Information Technology Reference
In-Depth Information
The above template class
List
looks similar to any other class definition,
except that the first line denotes it as a template for use with the undefined type
X
.
This identifier,
X
, is the placeholder that will be replaced when the template class
is instantiated by some concrete data type that is to be acted on. The
append
method, for example, will then accept a single argument containing that data type
and add it to the list. The data type of the element will be declared upon the
creation of a list object, as in the following example.
In the first bit of code above, we create a class
Apple
. Next, we instantiate the
template class
List
into an object named
appleList
, to be used with the data
type
Apple
. We go on to create two instances of the
Apple
class,
appleA
and
appleB
, and use the
append
method derived from the template class to add
them to
appleList
. The statement
List for Apple appleList
substitutes
every occurrence of the placeholder
X
from our template class with the data type
Apple
for
appleList
. In this way, templates provide for yet another level of
abstraction by allowing for dynamic data type declarations with classes.
2.5.3 Generic Components
The principle of abstraction lies at the heart of component based software engi-
neering. Abstraction allows for the creation of generalized components which can
be modified and implemented for specific situations. This generalization enhances