抽象データ型は、一連の値と、それらに対して許可された操作の集合を定義し、動作によってのみ記述されます — コードではなく。実装は、その動作を実現する具体的な構造(配列、連結リスト、木)です。
同じADT、異なる実装
text
ADT "Queue": enqueue, dequeue, peek (FIFO contract)
Implementation A: array + two indices (ring buffer)
Implementation B: doubly linked list with head/tail pointers
Both honor the SAME contract; cost profiles differ.
なぜ分離するのか
python
# Callers depend on the CONTRACT, not the internals
class Stack:
def push(self, x): ...
def pop(self): ...
def peek(self): ...
# Swap a list for a linked list internally — callers never change.
一般的なADTと一般的な実装
なぜ重要なのか
ADT(インターフェース)に対してプログラミングすることで、コードは柔軟に保たれます。後で実装を最適化できますし、呼び出し元を壊すことはありません。
また、推論を鋭くします — 最初にどの動作が必要かを決めて、その動作を提供する最も安価な構造を選択します。
