AlgoViz

Encapsulation

easy 35 min

Put 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

BankAccount- holder: str- number: str- balance_cents: int- frozen: bool- day: int- taken_today: int+ balance_cents: int+ masked_number: str+ can_withdraw(...): bool+ deposit(cents)+ withdraw(cents, day)+ freeze()- why_not(...): str

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

MemberWho asksWhy it is safe
balance_centsscreen, receiptan integer, the caller gets a value
masked_numberreceiptthe full number never leaves
can_withdrawscreen, to grey out a buttona yes or no, no state changes
deposit, withdrawcash machine, transferthey check everything first
freezefraud teamno arguments, cannot be misused
no set_balancenobody has a reasona correction is a deposit with a note