Lessons in this course (8)

1. From requirements to classes — nouns, verbs and responsibilities

Basic★ Must knowLesson 1 of 8

Technique: write the use cases as sentences, underline nouns (candidate classes) and verbs (methods), then assign each responsibility to the class that owns the data it needs (Information Expert).

Example — "A member borrows a book copy from the library; the system records the due date and charges a fine if it is returned late."

Nouns → classesVerbs → methodsOwner
Member, Book, BookCopy, Library, Loan, Fineborrow, return, record due date, charge fineLibrary.Borrow(member, copy) creates a Loan; Loan.Return(date) computes overdue days; IFinePolicy.Calculate(loan) gives the amount

Guidelines:

  • Each class has one reason to change (SRP).
  • Prefer composition ("has-a") over inheritance; inherit only for true "is-a" with shared behaviour.
  • Depend on interfaces for things that vary (payment methods, pricing rules, notification channels).
  • Use enums for fixed sets of states/types; use State/Strategy when behaviour differs per state/type.
  • Make invalid states impossible (private setters, constructor validation, value objects like Money).