Notes
The requirements
Here is the problem. Members book a spot in a fitness class. Each class has a trainer, a start time and a capacity. When a class is full, members can join a waitlist. If someone cancels, the first waiting member gets the spot and is told about it. Basic plan members can book at most three classes a week, and premium members have no limit. Read it twice before you design anything.
- Members book a spot in a
- fitness class. Each class has
- a trainer, a start time and a
- capacity. When a class is
- full, members can join a
- waitlist. If someone cancels,
- the first waiting member gets
- the spot and is told. Basic
- plan members may book at most
- three classes a week; premium
- members have no limit.
Step 1: clarify
Step one, clarify. Ask the questions whose answers change the classes. Can a member book the same class twice? No. Does a waitlist place count toward the weekly limit? Only once it turns into a real booking. Are payments and cancellation deadlines in scope? Not today. One gym, but several front desks working at once, so we must think about threads. Write the answers down. They become your rules and your test cases.
- can a member book the same
- class twice? no
- does a waitlisted spot count
- toward the weekly limit?
- only once it is confirmed
- cancel deadline? out of scope
- payments? out of scope
- one gym or many? one
- many front desks at once? yes
Step 2: nouns
Step two, underline the nouns. Member, spot, fitness class, trainer, start time, capacity, waitlist, plan, basic and premium, week, limit, gym, front desk, and the app that is implied. Each noun is only a candidate. Some will become classes, some will become fields of other classes, and some will disappear. The point of this step is to make sure nothing important is missed before you start filtering.
- member, spot, fitness class,
- trainer, start time,
- capacity, waitlist, plan,
- basic plan, premium plan,
- week, limit, gym, front desk,
- app (implied)
- nouns hint at classes
- and fields; they are not
- the answer yet
Step 3: filter
Step three, filter. A noun with no behaviour and one value is a field, not a class: start time and capacity belong to the class session. A spot is not an object, it is capacity minus bookings. The week is a rule about dates. There is only one gym, so it adds nothing. The app is outside our model. Basic and premium are values of one enum. We keep Member, GymSession, Trainer, Booking and MembershipPlan. We call it GymSession because class is a Java keyword.
- start time, capacity: fields
- of the class, not classes
- spot: just capacity minus
- bookings, a number
- week: a date range rule
- gym: only one, so skip it
- app: outside the model
- basic, premium: enum values
- keep: Member, GymSession,
- Trainer, Booking, Plan
Core entities
Now find the core. In almost every booking problem, there are two central entities: the thing being booked, here a GymSession, and the record that someone booked it, here a Booking. Everything else hangs off them. A member holds a plan. A session is led by a trainer. Getting these two right is most of the design. Parking spots and tickets, seats and reservations, rooms and stays all follow the same shape.
Step 4: verbs
Step four, the verbs. Book, cancel, join the waitlist, get the spot, be told, and may book at most three. Each one becomes a method. Book and cancel, add to waitlist, promote next, notify, and allows another booking. The verb only tells you what happens. The harder and more important question is which class should own each method. That is the next step, and where good designs and poor designs split.
- book a spot -> book()
- cancel -> cancel()
- join waitlist -> addToWaitlist()
- get the spot -> promoteNext()
- is told -> notify()
- may book at most three
- -> allowsAnother()
- a verb tells you WHAT;
- the owner is the next step
Step 5: responsibilities
Step five, responsibilities. The simple rule is: give each job to the class that already has the data it needs. Is there room? The session knows its capacity and bookings, so it decides. Who is next in line? The session owns its waitlist. What is the weekly limit? The plan knows its own number. Then one service, BookingDesk, coordinates a whole booking from start to end, without holding the rules itself.
- is there room?
- GymSession: has capacity
- and its bookings
- who is next in line?
- GymSession: owns waitlist
- weekly limit?
- MembershipPlan: knows the
- number for each plan
- run the whole booking?
- BookingDesk (a service)
Step 6: relationships
Step six, connect them and add numbers. Each member has exactly one plan. Each session is led by one trainer and holds between zero and capacity bookings, plus an ordered waitlist of members. Each booking points to one member and one session. The booking desk uses the sessions and a notifier. That is a class diagram. Notice the sizes are small. A first design should fit in about seven boxes.
Step 7: code it
Step seven, write the core code. GymSession's book method checks the duplicate rule and the capacity rule, because it owns that data. If the session is full, the member goes to the end of the waitlist. Otherwise the member is added and a Booking is returned. The method is synchronized, so two front desks booking the last spot at the same moment cannot both get it.
synchronized Optional<Booking>
book(Member m) {
if (attendees.contains(m)) throw ...;
if (attendees.size() == capacity) {
waitlist.addLast(m);
return Optional.empty();
}
attendees.add(m);
return Optional.of(new Booking(m, name));
} Step 8: test a flow
Step eight, test the design by walking one scenario. Ria is on the basic plan and already has three bookings this week. The desk asks Ria's plan whether a fourth is allowed. The basic plan says no, so the desk refuses before touching any session. Walking a flow like this, out loud, quickly shows a missing method, a rule in the wrong place, or a class nobody uses.
The cancel flow
Now the trickier flow. The evening spin class is full, and Kabir is first on the waitlist. Arjun cancels. The desk calls cancel on the session. The session removes Arjun, takes Kabir off the front of its waitlist, and creates a booking for him in the same step. The desk then asks the notifier to tell Kabir he is in. Because the session does all of this inside one lock, no new booking can steal the freed spot first.
Common mistakes
Watch for the common mistakes. One god class, a gym manager, that does everything. Or the opposite, a class for every noun, like Capacity and StartTime. Data classes with only getters, while every rule sits in one huge service. Reaching for design patterns before the entities are clear. And skipping the clarifying questions. The fix is the same each time: rules live with the data they use, and services only coordinate.
- GymManager does everything
- classes for every noun
- (Capacity, StartTime)
- data classes with all the
- rules in a service
- patterns before entities
- no clarifying questions
- fix: rules live with data,
- services coordinate
Say it like this
In the interview, narrate the method. Say: the core entities are GymSession and Booking. Capacity and the waitlist live in GymSession because it owns that data. The weekly limit lives in the membership plan. BookingDesk coordinates the flow and sends notifications. In under a minute, the interviewer knows your classes, your reasons, and where every rule lives. Then you can start coding with confidence.
- "The core entities are
- GymSession and Booking.
- Capacity and waitlist live
- in GymSession because it
- owns that data. The weekly
- limit lives in the plan.
- BookingDesk coordinates and
- sends notifications."