Ownership of Objects and Classes

Simple ownership

Lets start with a bowling ball and a bowler. I’ll be using Groovy for examples in this post, because I can use strong typing only where I need to and not have to write as much boilerplate as I would in Java.

A more complicated case

To illustrate, let’s formulate use cases and constraints regarding bowling balls. Here they are:

  1. A bowling ball has a brand and serial number
  2. A Bowler’s bowling ball can have a pet name
  3. A bowling ball can be owned by a Pro Shop or a Bowler, but not both
  4. A bowling ball’s properties can be mutated, but only by the Pro Shop or Bowler
  5. A bowling ball cannot exist in and of itself, it must belong to Pro Shop or Bowler

Ownership by Instance

Both entities, ProShop and Bowler, need instances of BowlingBall, but if BowlingBall is a static private inner class of Bowler, that can’t happen, and I would rather not have to maintain two separate but identical class definitions in both owning Classes.

Ownership by type

So I want to point out here that we’ve subtly transitioned from talking about objects owning other objects to object types owning other types. Let’s take a look at the new code listing. There’s a lot of additional goings-on here, which I will explain in the comments following.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jeff Lowery

Jeff Lowery

Wrote software for a living. Now I do it for fun.