# Finding Entities and Relationships — High-Level & Low-Level Design Interview Problems

Source: https://www.skillbyai.com/en/design-interviews/lld-entities

> Nouns, verbs and associations.

## Nouns become classes, verbs become methods

Read the requirements and underline **nouns** (candidate classes or attributes) and **verbs** (candidate methods or services). Decide which nouns have behaviour and identity (classes) and which are just values (enums or value objects). Then name the **relationships**: *is-a* (inheritance or interface implementation), *has-a* (composition when the part cannot exist alone, aggregation when it can) and *uses* (a dependency passed in). Keep the first model small, cover the main use cases, and add classes only when a responsibility needs a home.

## From requirements to classes

Good LLD answers find the right nouns and verbs, apply a few principles and use patterns only where they fit.

![Three ideas: identifying entities and relationships, SOLID in practice, and four patterns that recur.](assets/figures/design-interviews/section-2-map.svg) — Figure 2.1 — Entities, principles and patterns in LLD.

## From a requirement to a model

A library example.

```text
Requirement: "Members borrow book copies; each copy has a barcode.
 A member may hold at most 5 copies; late returns incur a fine."

Nouns  -> Member, Book, BookCopy, Loan, Fine
Verbs  -> borrow(), return(), calculateFine()

Relationships
  Book 1 --- * BookCopy        (composition: copies belong to a title)
  Member 1 --- * Loan          (a member has loans)
  Loan * --- 1 BookCopy        (each loan is for one copy)
  FinePolicy <- LoanService    (uses: injected dependency)
```

## Prefer composition

Reach for inheritance only for a true is-a relationship with shared behaviour; otherwise compose objects and depend on interfaces.

**Quiz:** A Loan that holds a reference to a BookCopy is best described as:

- [ ] Inheritance from BookCopy
- [x] An association between Loan and BookCopy
- [ ] A static utility method
- [ ] An enum value

*Answer:* An association between Loan and BookCopy. A loan refers to a copy; it is not a kind of copy.
