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
Design
One class with parts, small contracts, and clear rules for replacing.
The point: give each variable part a contract with the few methods the phone really calls.
The phone and its parts
1/4The whole
One class. It answers charge() itself and hides who does the work.
depends onhas (lives on)implements
Rules to agree on
| Question | Answer here |
|---|---|
| May the battery be missing? | No. A phone is built with one |
| Who may replace it? | Anyone who holds the phone, through swapBattery |
| What happens to the old one? | It is returned to the caller |
| May two phones hold one battery? | No, but the type does not stop it. C++ unique_ptr does |
A useful part contract
- Two or three methods
- Named after what the whole needs
- No reference back to the whole
A disguised parent
- The part calls back into the phone's fields
- Thirty forwarding methods
- The phone asks which battery class it has