Publishing a Valid Replacement State

A queue stores its fields together in an immutable state record, meaning the record cannot be modified after creation. Its current record satisfies the class invariant. An append method receives a text request. Parsing the text and constructing a proposed record can throw exceptions. Publishing a record uses one non-throwing reference assignment. In this sequential implementation, arrange the successful-operation steps so preparation failures leave the current record untouched and a notification callback can inspect only a validated state. If validation fails, the method rejects the proposal without publishing it.

  1. 1.

    Construct a separate proposed record from the parsed values and the current record.

  2. 2.

    Parse the text request into the values needed to construct a proposed record.

  3. 3.

    Invoke the notification callback, allowing it to inspect the published state.

  4. 4.

    Publish the accepted record by replacing the current-record reference.

  5. 5.

    Check every class-invariant clause against the proposed record and accept the proposal.

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

More Class Invariants Questions: