Abstract Data Types: Specifying Behaviour

When you use a software component, you need to know what its operations do before you need to know how its code works. An abstract data type, or ADT, specifies a set of possible values and operations through their observable behaviour. It lets you reason about a component using its rules, even when someone changes the storage or algorithms behind those rules.

Describe what a user can observe

An ADT describes the information its operations can reveal, the requests you can make, and the effects of those requests. The word abstract means that the description leaves out implementation choices that do not affect the specified behaviour.


Consider a resource-pool ADT that tracks how many identical permits are available. Its capacity is CC, and its current number of available permits is ff. The model deliberately tracks counts rather than permit identities.


0≤f≤C0 \leq f \leq C


The pool begins with f=Cf=C. It provides three operations: acquire permits, release permits, and report availability. A program using the pool is a client.


The client needs the meaning of each operation. It does not need to know whether availability is stored as one integer or calculated by counting free entries in an array.


A client uses public operations that can be supported by either counter storage or flag storage.


Make the operation rules precise

A contract states the conditions for using an operation and the behaviour promised when those conditions hold. A useful contract identifies inputs, returned results, and effects on the state. An operation's name alone rarely supplies enough information.


For the resource pool, suppose every requested quantity kk must be a positive integer. Let f′f' mean availability after the operation.


acquire⁡(k):(f′,result)={(f−k,true),k≤f,(f,false),k>f.\operatorname{acquire}(k):\quad (f',\text{result})= \begin{cases} (f-k,\text{true}), & k\leq f,\\ (f,\text{false}), & k>f. \end{cases}


This operation is all or nothing: it either acquires the entire requested quantity or acquires nothing. Insufficient availability is a specified failure result, rather than an invalid input.


Operation

Requirement before the call

Promised behaviour

acquire(k)

k is a positive integer

Return true and subtract k if enough permits exist; otherwise return false without changing availability

release(k)

k is a positive integer no greater than C − f

Increase availability by k and return no value

available()

None

Return f without changing the pool


A precondition tells the client which calls the contract covers. A postcondition tells the client what follows from such a call.


Example: A pool with capacity 12 begins with 12 permits available. Calling acquire(5) returns true and leaves 7 available. Calling acquire(9) next returns false and still leaves 7 available.



For release(k), requesting more than the number currently acquired violates its precondition. This specification makes no promise about that call; a fuller specification could explicitly require an error response.

Separate behaviour from implementation

An implementation turns the ADT's rules into executable code. For the same pool, one implementation might store the number of free permits. Another might store a flag for each permit and count the free flags.


These implementations can realize the same ADT if every permitted sequence of calls produces the specified results and state changes. Matching a few example outputs is useful evidence, but it does not establish agreement for every valid use.



A client that relies only on the contract can continue working when the implementation changes. A client that directly reads a storage field adds a dependency the ADT did not promise. Implementations can also have different running times or storage needs; any required performance guarantees must be stated explicitly rather than inferred from an operation's name.


What to remember

  • An ADT specifies values, operations, and observable behaviour.
  • Contracts distinguish valid inputs, returned results, and state changes.
  • Different implementations can satisfy the same specification.
  • Check agreement across permitted call sequences, including specified failures.

Example: Managing Equipment Availability

A fieldwork office uses an EquipmentPool ADT to track availability among 32 identical equipment kits. It tracks counts only, not kit identities. Initially, all 32 kits are available.


Its operation contract gives these rules. For a positive integer k, reserve(k) returns true and subtracts k from availability if enough kits are available. Otherwise, it returns false and changes nothing. cancel(k) adds k back and requires k not to exceed the number currently reserved. available() returns the available count without changing it.


a) A coordinator calls reserve(11), then reserve(8). How many kits are reserved in total?


Both requests can succeed, but check availability in sequence rather than treating each call as a new pool.


After the first request, availability is ________, because


f1=32−11=21 kits.f_1=32-11=21\text{ kits}.


The second request asks for 8 kits, which is within the 21 available. It also returns true. Availability becomes ________, because


f2=21−8=13 kits.f_2=21-8=13\text{ kits}.


The number reserved is the initial capacity minus the remaining availability:


r=32−13=19 kits.r=32-13=19\text{ kits}.


Answer: 19 kits are reserved in total.


b) Continuing from part a, the coordinator calls cancel(5), then reserve(16). What does reserve(16) return, and how many kits remain available?


There are 19 reserved kits before the cancellation, so cancelling 5 satisfies the requirement that the cancelled quantity not exceed the reserved quantity.


The cancellation increases availability to ________:


f3=13+5=18 kits.f_3=13+5=18\text{ kits}.


The next request asks for 16 kits. Since 16 is no greater than 18, reserve(16) returns true and subtracts the full requested quantity:


f4=18−16=2 kits.f_4=18-16=2\text{ kits}.


As a check, the number reserved is 19 − 5 + 16 = 30 kits, and 30 reserved plus 2 available equals the capacity of 32 kits.


Answer: reserve(16) returns true, and 2 kits remain available.


c) One implementation stores a single available-count integer. Another stores 32 flags, one per kit, and counts the free flags. Can both implement this EquipmentPool ADT?


Yes, provided each implementation obeys every operation rule for every permitted call sequence. The integer implementation subtracts or adds quantities directly. The flag implementation marks the requested number of free flags as reserved on success and reverses the requested number on a valid cancellation.


Both must return false without changing availability when a reservation cannot be filled. Both must make available() report the current number free. After the calls in parts a and b, each must therefore report 2 available kits.


The flag implementation may distinguish individual storage positions internally, but this ADT exposes only counts. Internal differences are acceptable when all specified observations agree.


Answer: Yes; either storage choice can satisfy the same ADT contract.

A campus printing service needs a component that tracks remaining printing credits. Which statement specifies observable behaviour suitable for an ADT contract?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A BoundedCounter abstract data type has a fixed limit chosen at creation, an operation increment() that adds one to the count, an operation reset() that sets the count back to zero, and an observer value() that returns the current count. Its specification contains three clauses. Name the kind of clause each one is.


The clause increment() may be called only when value() is less than limit is a of increment().


The clause after reset() completes, value() returns 0 is a of reset().


The clause 0≤0 \le value() ≤\le limit holds before and after every operation is an of the type.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A software team is documenting a resource-management component before selecting its storage strategy. Match each specification term to its meaning.

Match each term
A.

A requirement that the caller must satisfy before applying an operation

B.

The concrete arrangement of stored information used to realize the model

C.

Rules stating the requirements for a call and the behaviour promised when they hold

D.

A model of possible values and permitted operations described by their observable behaviour

Precondition

Abstract data type

Representation

Contract

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice