Abstract data types are used to describe a “generic” type of data, specifying how the data is stored and what operations can be performed on it (see object-oriented program-ming, list processing, stack, and queue).
For example, an abstract stack data type includes a structure for storing data (such as a list or array) and a set of operations, such as “pushing” an integer onto the stack and “popping” (removing) an integer from the stack. (For the process of combining data and operations into a single entity, see encapsulation.) Abstract data types can be implemented directly in object-oriented programming languages (see class, c++, Java, and Smalltalk).
One advantage of using abstract data types is that it separates a structure and functionality from its implemen-tation. In designing the abstract stack type, for example, one can focus on what a stack does and its essential func-tions. One avoids becoming immediately bogged down with details, such as what sorts of data items can be placed on the stack, or exactly what mechanism will be used to keep track of the number of items currently stored. This approach also avoids “featuritis,” the tendency to see how many possible functions or features one can add to the stack object. For example, while it might be useful to give a stack the ability to print out a list of its items, it is probably better to wait until one needs such a capability than to bur-den the basic stack idea with extra baggage that may make it more cumbersome or less efficient.
An abstract data type or its embodiment, a class, is not used directly by the program. Rather, it is used to create an entity (object) that is a particular instance of the abstract data type (for example, an actual stack that will be used to manipulate data). The data stored inside the object is not accessed directly, but through functions that the object receives from the abstract data type (such as the push and pop operations for a stack). (For more information about how such objects are used, see class.)
Because the abstract data type is not directly used by the program, the implementation of how the data is stored or manipulated can be changed without affecting programs that use objects of that type. This information hiding is one of the chief benefits of object-oriented programming. Another advantage is inheritance, the ability to derive more specialized versions of the abstract data type or class. Thus, one can create a derived stack class that includes the print-ing function mentioned earlier.
No comments:
Post a Comment