Lecture 12: Abstract Data Types(ADT)

1 What Abstraction Means

Here are some of the names that are used for Abstract Data Types:

  • Abstraction
  • Modularity
  • Encapsulation
  • Information hiding
  • Separation of concerns

1.1 User-Defined Types

  • The key idea of data abstraction is that a type is characterized by the operations you can perform on it.
  • The user of the type would not need to worry about how its values were actually stored.

2 Classifying Types and Operations

  • Creators create new objects of the type. A creator may take an object as an argument, but not an object of the type being constructed.
    • A creator operation is often implemented as a constructor, like new ArrayList()
    • A creator can simply be a static method instead, like Arrays.asList(). A creator implemented as a static method is often called a factory method.
  • Producers create new objects from old objects of the type. The concat method of String , for example, is a producer: it takes two strings and produces a new one representing their concatenation.
  • Observers take objects of the abstract type and return objects of a different type. The size method of List , for example, returns an int.
  • Mutators change objects. The add method of List , for example, mutates a list by adding an element to the end.
    • Mutators are often signaled by a void return type. A method that returns void must be called for some kind of side-effect, since otherwise it doesn’t return anything.
    • Not all mutators return void. For example, Set.add() returns a boolean that indicates whether the set was actually changed. In Java’s graphical user interface toolkit, Component.add() returns the object itself, so that multiple add() calls can be chained together.

3 Designing an Abstract Type

  • It’s better to have a few, simple operations that can be combined in powerful ways, rather than lots of complex operations.
  • Each operation should have a well-defined purpose, and should have a coherent behavior rather than a panoply of special cases.
  • The set of operations should be adequate in the sense that there must be enough to do the kinds of computations clients are likely to want to do.
  • It should not mix generic and domain-specific features.
    A Deck type intended to represent a sequence of playing cards shouldn’t have a generic add method that accepts arbitrary objects like integers or strings. Conversely, it wouldn’t make sense to put a domain-specific method like dealCards into the generic type List.

4 Representation Independence

  • Representation independent means that the use of an abstract type is independent of its representation (the actual data structure or data fields used to implement it), so that changes in representation have no effect on code outside the abstract type itself.

Reference

[1] 6.005 — Software Construction on MIT OpenCourseWare | OCW 6.005 Homepage at https://ocw.mit.edu/ans7870/6/6.005/s16/

你可能感兴趣的:(软件工程)