liveBook · Manning

Contract

As explained in Advanced Algorithms and Data Structures

An API is a defined interface through which software requests operations or data from another software component or service.

An API contract defines how clients interact with a component: its available operations, their inputs and outputs, and the guarantees they provide. It separates what clients can rely on from how the component is implemented. section 1.1.2 section 2.4

Specify a data structure by moving from its public contract toward its implementation:

  • List operations, focusing on their inputs and outputs. section 1.1.2
  • Describe high-level behavior, then implementation details and method performance. section 1.1.2
  • Keep client-facing guarantees distinct from internal invariants, data models, and algorithms. section 2.4

A shared service interface

An ordering service that works with multiple retailers needs a common API for placing orders; each retailer can use an adapter to connect its internal software to that interface. section 11.1.1

Table 2.1 API and contract for priority queue (view table figure)

Abstract data structure: Priority queue

API

class PriorityQueue {
  top() → element
  peek() → element
  insert(element, priority)
  remove(element)
  update(element, newPriority)
  size() → int
}

Contract with client

The top element returned by the queue is always the element with highest priority currently stored in the queue

A priority queue contract lists its methods and guarantees that the returned top element has the highest stored priority. section 2.4

How Advanced Algorithms and Data Structures covers it

How deep it goes
Distinguishes client-facing guarantees from internal invariants, data models, and algorithms.
Examples
Uses priority queue operations and a shared ordering interface for multiple retailers.
What you can do after
Build it from scratch by specifying operations, behavior, implementation details, and performance.
How it connects
Connects abstract data types and data structures with invariants, data models, and algorithms.

Also explained in

Compare how 44 books explain contract →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.