IS-A and HAS-A
easy 40 minIS-A says that an object is a kind of something and can stand in for it.
Watch lesson4:45
Problem
From nouns to relations: ask the right questions before drawing arrows.
The point: write the sentences first, and test each of them before it becomes an arrow.
The task
A phone shop
The shop sells three phone models. Customers choose battery and camera, some models have a pen, batteries are replaced in repair, and the shop tests phones without real batteries.
Sentences and what they tell us
| Sentence | Test | Relation |
|---|---|---|
| A slab phone is a phone | For life, and the shop sells phones | IS-A, or only a value model |
| A phone has a battery | Replaced in repair | HAS-A, replaceable |
| A fast battery is a battery | Charges, has a capacity | IS-A (interface) |
| A pen phone is a phone with a pen | A part again | HAS-A, optional |
| A repair shop uses a phone | For one job, not kept | USES-A |
Three phone models differ in brand, name and parts. What are they?
BreaksOne subclass per model and option
- How
SlabPhoneBigBatteryPen.- Limit
- Combinations multiply, and a repaired phone has the wrong class.
- Choose when
- Never.
WorksOne subclass per model, parts as fields
- How
SlabPhone extends Phone, with a battery field.- Limit
- Good only if models differ in behaviour.
- Choose when
- A folding phone really opens and closes.
StrongOne class, with model as data and parts as fields
- How
Phone(model, battery, camera).- Limit
- A later model with special behaviour needs a new part or a subclass.
- Choose when
- Models differ in values and parts, as here.