Class design: state and behaviour 00:00:00 Make a prediction You will give one object a narrow set of operations that enforce its invariant, including what it returns when an operation must fail. Two gates scan the same unused ticket. Is a public used field plus an admission method enough to guarantee one admission? No. Anyone can bypass the method by changing the field, and simultaneous checks can both see unused. Give admission a single owner and perform the state transition atomically where concurrent scans are allowed. 00:00:32 Asha, Max and the cabin In a cabin, a kettle belongs near the cooking station and the compass belongs with the navigator. Keeping both in an unlabelled heap means every crew member must know where everybody's tools belong. An object owns the facts its operations must protect. Give it a job first, then place the fields and validation needed for that job inside. A field that anyone can freely rewrite is hardly owned at all. Class design is the step between a list of entities and code. For each class you decide four things: what it remembers (state), what others may ask or tell it (behaviour), what is always true (invariants) and how it says no (errors). Bodies are cheap to change, signatures are not. Once three classes call book(member), adding a time to it changes all three. Five minutes on signatures save the last fifteen minutes of a round. 00:01:33 Give the ticket one owner The sale gathers payment and issues a ticket that starts unused. Write that starting condition before picking the fields. Put the transition to used behind the ticket's admission operation. A caller cannot mark the ticket used and then forget to record admission. When a second gate scans the same identifier, the owner must reject a second transition. Test that the ticket still records one admission. Test ticket validation by scanning the same ticket twice. The first scan moves the ticket to used; the second must refuse entry. A public setter could move the ticket back to unused without a sale. Put the transition on the object that can enforce that history. 00:02:15 Design a small queue A queue with capacity two accepts two jobs and rejects a third. Its capacity and size belong together because inserting a job must check the limit. Returning the internal list lets another object bypass the check, so expose an enqueue operation and a read-only size. Enqueue A, then B. Ask the queue to accept C. A full queue must retain both earlier jobs and report its rejection. If a caller removes A, one slot opens and C becomes legal. Returning the internal list would let anyone bypass that same limit. A public boolean setter lets a caller put a used ticket back into the unused state. Having a class did not protect its rule. For a simple immutable value, constructor validation plus read-only fields may be a better design than a lifecycle of mutable setters. Java BlockingQueue. Four method families for one situation: add throws, offer returns false, put waits, and a timed offer gives up. The name tells you how it says no. Java LocalDate. An immutable date. There is no setter: plusDays answers with a new date, so a wrong month can never appear later. 00:03:34 Try an interview follow-up When do you store a derived value after all? When working it out is measured to be too slow, or when it must not change later, such as the price at the time of sale. Then one method updates both, and the equality is an invariant with a test. Design a store return. A customer hands over a receipt. Who should calculate the refund? Walk from a returned line item to the original paid price and its return policy. Put the calculation where those facts are available. Then ask the object to reject a second refund of the same line. 00:04:15 What to take with you Read each requirement as remember, do, or rule. Put it on the class that owns the data. Drop every field that can be worked out from another one. Write signatures before bodies: the name says the intent, the result says every normal outcome, errors say what is refused. Check first, change last, so a refused call changes nothing.