Encapsulation
easy 35 minPut the data and the methods that work on it into one unit, and let outside code reach the data only through those methods.
Watch lesson4:51
UML design
What is public, what is private, and why each public member earns its place.
The point: design the public list from the callers' questions. Everything else is private.
The cash machine asks the account
1/3Six private fields
Holder and number are set once. Balance, day and the sum taken today change only together.
usesdepends on
Every public member and its caller
| Member | Who asks | Why it is safe |
|---|---|---|
balance_cents | screen, receipt | an integer, the caller gets a value |
masked_number | receipt | the full number never leaves |
can_withdraw | screen, to grey out a button | a yes or no, no state changes |
deposit, withdraw | cash machine, transfer | they check everything first |
freeze | fraud team | no arguments, cannot be misused |
no set_balance | nobody has a reason | a correction is a deposit with a note |