Encapsulation Through Operations

Encapsulation gives callers operations with a declared meaning while allowing the object's representation to evolve.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Model a reservation
  2. Provide observations without writable access

Model a reservation

A booking object can expose reserve and release methods that preserve nonnegative available seats. Setting its counter directly from several callers can distribute the invariant. The representation invariant identifies the properties each supported update must maintain.

Provide observations without writable access

Return a count or a stable snapshot when a caller needs to display seats. Returning the mutable internal list can let that caller invalidate the object's assumptions. See ownership of inputs.[1]

References

  1. Python Software Foundation, Python Tutorial: Classes ↩