Comet sketches a fan in her notebook: three speed buttons and a switch. "Could this be a class?" she asks. "It knows things and it does things."
Wren adds a question underneath. "What does the evidence say?" he asks. "If anyone could set the speed to 99, what would happen to the fan?"
Nova draws a box around the fan with three small windows. "How can I help?" she asks. "Decide which values stay inside, and which buttons outside users get."
Comet tears out a fresh page for you. "Find a family member, lead programmer. Pick a device at home."
Design a class before writing it, with its attributes and behaviors. Encapsulation keeps the details hidden from other classes.
A real device works the same way. You press its buttons, but you cannot reach in and set the motor to any speed.
| Fan (class diagram) | Hidden or public? | Why |
|---|---|---|
| _speed (0 to 3) | Hidden | Only the buttons may change it |
| _is_on | Hidden | Changed by the switch |
| press_speed(n) | Public | A button anyone may press |
| get_speed() | Public | Lets others read the speed |
Strong design work. Next week Pip starts carrying samples, and objects begin working together.