Designing with Objects: Deciding What Each Class Owns · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Give every class one clear job

Computing · Programming · ages 16-18
Name ______________________   Date ____________
  1. Where do the responsibilities of a class come from?

    • the longest words in the story
    • the order nouns first appear
    • the verbs attached to its noun in the story
  2. A library story mentions books, members and lending. Which are the candidate classes?

    • Book, Member and Loan
    • borrow, lend and return
    • the library building
  3. If a class job needs the word and to describe, it is probably two classes.

    Circle one:   True   False

  4. A class Library holds book lists and also computes late fines with member discounts. Half its methods touch books, half touch money. What does this symptom say?

    • the class is perfectly sized
    • the class has taken on two unrelated jobs and should split
    • the methods need longer names
  5. A designer must place the due date. Book rarely touches it while Loan reads it daily. Where does it belong?

    • in Loan, which uses it most
    • in Book, since books are lent
    • in both, copied each time
  6. After splitting a two-job class, how should the two new classes relate?

    • they share nothing and duplicate the data
    • one absorbs the other entirely
    • each owns its group and they connect through a clean link
  7. A single class manages member signups and book repairs, and its name fits neither half. What is the repair?

    • rename it to something vaguer
    • split signup data and methods from repair data and methods into two classes
    • move everything into the constructor
  8. A designer is tempted to store the member address list inside Book because checkout prints it. How do you argue the boundary?

    • printing decides ownership, so Book keeps it
    • Member uses addresses constantly while Book only borrows one for a moment, so Member owns it
    • both classes must store full copies
LightMySky · lightmysky.comW1-mt_De_gpvpOzg-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Give every class one clear job W1-mt_De_gpvpOzg-s1

  1. the verbs attached to its noun in the story · What the thing does in the story is what the class must do in code.
  2. Book, Member and Loan · Nouns name the things, so they suggest the classes. Verbs suggest duties.
  3. True · One clear job fits one sentence. Two jobs need splitting.
  4. the class has taken on two unrelated jobs and should split · Methods splitting into two data groups is the mark of two classes in one costume.
  5. in Loan, which uses it most · Data lives with the class that uses it most, beside the methods that touch it.
  6. each owns its group and they connect through a clean link · Each class keeps its own data and methods, joined only where they must talk.
  7. split signup data and methods from repair data and methods into two classes · A name fitting nothing proves the split. Each half becomes a class with one job.
  8. Member uses addresses constantly while Book only borrows one for a moment, so Member owns it · Momentary use loses to constant use. Ownership follows the heaviest user.
Worksheet · LightMySky