Sub topic 3
Describing an ADT by its operations
This chapter assumes you can read Python classes and know that an abstract data type (ADT) is defined by what it does, not by how it stores data. Here you will learn to sort an ADT's operations into categories. Doing so gives you a checklist for describing any ADT completely.
Why operations define the type
A client of an ADT never sees the stored data. It sees only the operations. So the operations are the type. To describe an ADT, list every operation, give each one a signature, and say what kind of operation it is.
A signature gives the operation's name, its inputs, and its output. For a stack, push(item) takes an item and returns nothing. pop() takes nothing and returns an item.
The three categories
Almost every operation falls into one of three categories.
| Category | What it does | Stack example |
|---|---|---|
| Constructor | Creates a new value of the type from nothing, or from values of other types | `ListStack()` |
| Mutator | Changes the value of an existing object | `push(item)`, `pop()` |
| Observer | Returns information about a value without changing it | `peek()`, `is_empty()` |
A fourth category, the producer, builds a new value from existing values of the same type. Examples are "abc".upper() and a.union(b) on sets. Immutable types such as strings rely on producers because they have no mutators. Mutable types such as our stack usually need none.
Some operations belong to two categories. pop() changes the stack and also returns the removed item, so it is both a mutator and an observer. Python's list.pop works the same way. Many designers accept this overlap for convenience. Other designers split the two jobs into separate top() and remove_top() operations. Whichever you pick, state it in the specification.
Seeing the categories in code
The comments below label each operation in ListStack.
class ListStack:
def __init__(self): # constructor
self._items = []
def push(self, item): # mutator
self._items.append(item)
def pop(self): # mutator and observer
if not self._items:
raise IndexError("pop from empty stack")
return self._items.pop()
def peek(self): # observer
if not self._items:
raise IndexError("peek at empty stack")
return self._items[-1]
def is_empty(self): # observer
return not self._items
s = ListStack()
s.push("a")
print(s.is_empty()) # False
print(s.peek()) # a
print(s.pop()) # a
print(s.is_empty()) # TrueThe sequence of calls follows a pattern. A constructor creates the object, mutators change it, and observers report what happened.
Why the categories matter
The categories help in three ways.
- Completeness. A type with no observers is useless, because nothing can be learned from it. A type with no constructor cannot be created. Check that each category is present.
- Specification. Behavior is mostly stated as how mutators change what observers report. For example, after
push(x),peek()returnsx. The next chapter builds on this. - Testing. Tests work by calling mutators and then checking observers. If an observer is missing, you have no way to check the result.
Common mistakes
- Listing only mutators and forgetting the observers needed to verify them.
- Writing a signature that names a storage detail, such as
get_list(). This ties the interface to one implementation. - Leaving out error cases.
pop()on an empty stack needs a stated outcome, hereIndexError.
Exercise
An ADT Counter has the operations Counter(), increment(), reset() and value(). Classify each one as a constructor, mutator, or observer.
Show answer
Counter() is a constructor. increment() and reset() are mutators. value() is an observer.
Exercise
A colleague proposes peek() that also removes the top item. Which category rule does this break, and what is the effect on clients?
Show answer
It breaks the rule that an observer must not change the abstract value. Calling peek() twice would return different items, so clients could not inspect the top safely.