Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Holders and delegation

โญโญโญ advancedโณ 10 min read๐Ÿ“ Lesson 214 of 256

The truth lives in held objects; the holder asks, never copies โ€” duplicating state drifts.

Object interaction is the FRQ2 finale: a class that HOLDS other objects and drives them. The two patterns:

  • Delegation loop: Library.playAll() calls play() on every held Song. The Library stores no play counts itself โ€” the truth lives in the held objects, and the Library only asks.
  • Aggregation query: likedCount() asks each held object a boolean and counts. One accumulator over a for-each.

The design smell to avoid: duplicating a held object's state in the holder (a Library that also tracks play counts will drift). When the story says the holder "knows about" the songs, it means it holds references โ€” not that it copies their fields.

Constructors of holders start empty (new ArrayList<>()), and nothing else happens there โ€” a constructor that creates data the caller should pass in is answering the wrong FRQ.