Objects and behaviour
What is information hiding?
On this page
Information hiding arranges a module around a decision likely to change. A schedule service can expose next_departure(stop, instant) while privately changing its index from a sorted array to a tree. Its boundary succeeds if the change leaves callers and their tests alone.
Worked example
Pythonfrom bisect import bisect_left
class Departures:
def __init__(self, minutes):
self._sorted = sorted(minutes)
def next_at(self, minute):
i = bisect_left(self._sorted, minute)
return self._sorted[i] if i < len(self._sorted) else None
board = Departures([25, 3, 14])
assert board.next_at(14) == 14 and board.next_at(26) is NoneExplore class relationships in the OOP Playground