M4 — State, Identity, and Object Creation

SENG 365 — Software Engineering

Neil Ernst

University of Victoria

2026-07-24

Learning Objectives

Learning Objectives

Reading: Robillard, 3rd ed. — Ch. 4, Object State (pp. 67–95).

  • Static and dynamic perspectives on software
  • Capturing and understanding state, including with state diagrams
  • Dealing with null values and states
  • Object identity vs. equality
  • The Singleton pattern and where it is useful

State

State and its Importance

  • Concrete state: the object at runtime.
  • Abstract state: an abstraction of the potential state space (e.g. “positive account balance”).
  • Choosing the right abstract states helps the concrete implementation.

State Diagrams

Capture the state of a system simple enough to manage cruise control. Try modelling a course at UVic.

  • States reflect the world/system at a point in time — usually nouns.
  • Transitions are the verbs.
  • A transition may be modelled as a state if it has domain importance (a question of abstraction level).
  • Keep the distinction between states of the world and states of the system capturing that world.

Null Values and Optional

  • Dealing with the absence of a value without null-pointer surprises.
  • Video: using Optional.
  • Recall Hoare’s “billion-dollar mistake.”

Identity and Equality

Object Identity

debugger and physical locations

  • Identity: typically the memory location on the heap.
  • Java uses references: two objects are == if the references point to the same id.
  • Two Aces are not == — different addresses.

Equality

  • We want to say two “Ace of Clubs” are the same card → override equals() and hashCode().
  • Equality is programmer-defined.
  • Default Object.equals() compares ids (==) — often not what our design needs.
  • A good hashCode() combines Rank + Suit.

Unique vs. Immutable

  • Unique: design so two identical cards are impossible (no cheating!). Hard to guarantee — Flyweight and Singleton attempt it.
  • Immutable ≠ unique: a unique Car can still be changed (e.g. add an engine).

Controlling Object Creation

Flyweight Pattern

  • We don’t always want new objects. For invariant, immutable state, value objects are far more efficient than thousands of objects.
  • The Flyweight factory checks whether an object with this property already exists.
  • Problem: avoid large numbers of objects. Solution: an immutable Flyweight managing invariant state.
  • Examples: characters in word processing, primitive sprites in graphics.

Singleton Pattern

  • Prevent new instances of a class (as opposed to unique instances).
  • Manage access to shared global state — e.g. the state of a game of chess or solitaire.
  • A single instance of a class, vs. unique instances of a class (Flyweight).
  • Video: implementing Singleton.

Closures

Closures

  • Anonymous classes/functions (lambdas) have a strange scope: they copy the current state into a new object.
  • We like anonymous classes because (for small model problems) they reduce clutter and aid encapsulation.
  • A closure is a method definition together with references to its local environment variables.
  • “The value in the enclosing scope is remembered even when the variable goes out of scope.”

In-Class Activities

Day 1

  • Guest lecture; Q&A
  • Refactoring? Code smells (preview)

Day 2

  • Group discussion

Day 3

  • Office hours