Notes
The problem
Here is the problem. The interviewer says: design a clinic appointment system. Most people start drawing classes at once. Twenty minutes later they learn that receptionists also book for walk in patients, that a full day needs a waitlist, and that payment was never in scope. The classes were fine, but for the wrong system. A use case diagram makes you ask these questions first, while changing your mind is still free.
- questions nobody asked:
- can a patient book alone,
- or only the front desk?
- what if every slot is full?
- who checks the patient in?
- does the doctor write notes
- here, or somewhere else?
- is payment in scope?
- skip them, and you design
- the wrong system, very well
The idea
The idea in one sentence: a use case diagram shows who interacts with the system and which goals they can reach with it. It has only a few pieces. Actors are stick figures outside the system. The system boundary is a box with the system name at the top. Use cases are ovals inside the box, each one a goal. Lines connect actors to the use cases they take part in. Three special relations, include, extend and generalization, add detail.
- actor: a role outside the
- system (stick figure)
- system boundary: a box with
- the system name on top
- use case: a goal, drawn as
- an oval inside the box
- association: a line from
- actor to use case
- relations: include, extend,
- generalization
Primary actors
Start with the primary actors. These are the roles that come to the system to get something done. Our clinic has three: the patient, the receptionist and the doctor. Notice that an actor is a role, not a person and not a job title in the company chart. A doctor who books her own check up is acting as a patient for that moment. Primary actors are usually drawn on the left side.
Secondary actors
Secondary actors are other systems that our system needs, but that do not come to it with a goal of their own. The SMS service sends booking reminders, and the payment gateway charges a card. They are outside our box, so they are actors, usually drawn on the right. Our own database is not an actor. It lives inside the boundary, because we build it and we own it.
- SMS Service
- sends booking reminders
- Payment Gateway
- charges the patient card
- they do not start goals;
- the system calls them to
- finish a goal
- not an actor: our own DB
Use cases are goals
Now the ovals. Each use case is a goal, named with a verb and a noun: book appointment, cancel appointment, check in patient, record consultation, pay bill. Use this test: when the actor finishes it, do they walk away with something of value? Booking passes. Click save, enter name, or open the form do not. Those are steps inside a goal. Even log in is usually a step, not a goal, because nobody visits a clinic app just to log in.
- goals (good):
- Book appointment
- Cancel appointment
- Check in patient
- Record consultation
- Pay bill
- steps (not use cases):
- Click save, Enter name,
- Open form, Log in
Associations
Associations are plain lines from an actor to a use case. They say this actor takes part in this goal. The patient can book, cancel and pay. The receptionist can book and cancel too, for walk in or phone patients, and is the only one who checks a patient in. The doctor records the consultation. These lines are your first permission table, and later each one becomes an access check in the code.
Include
Include pulls out a piece that a goal always needs. Booking always has to find a free slot, so we draw find free slot as its own oval, and a dashed arrow from book appointment to it, labelled include in double angle brackets. The arrow points at the part being used. Include is useful when the same piece is shared, for example if rescheduling also needs to find a free slot. Think of it as a method call that always happens.
Extend
Extend is the opposite: optional behaviour that only runs under a condition. If the doctor has no free slot that day, the patient can join a waitlist. We draw join waitlist with a dashed arrow labelled extend, pointing from the extension back to book appointment. The base use case is complete without it, and it does not know about it. You can write the condition on a small note, like: day is full.
Include vs extend
The two are easy to mix up, so compare them. Include always runs, and the arrow goes from the base to the part it needs. Extend runs only sometimes, and the arrow goes from the extension back to the base, because the base does not depend on it. A quick check: if you delete the extra oval, is the base goal broken? If yes, it was an include. If no, it was an extend.
- include
- always runs
- arrow: base to part
- base needs the part
- extend
- runs only if a condition
- holds
- arrow: extension to base
- base works without it
Actor generalization
Actors can inherit, just like classes. Every staff member, receptionist or doctor, can view the day's schedule. Instead of two lines to that use case, we add a parent actor called clinic staff, connect it once, and draw a solid line with a hollow triangle from each child to the parent. The children get everything the parent can do, plus their own goals. Use this only when it removes real repetition.
- use case: View day schedule
- Receptionist is a Clinic Staff
- Doctor is a Clinic Staff
- both can view the schedule,
- and each adds its own goals
- one line to the parent
- instead of two
The whole diagram
Here is the whole diagram. Primary actors on the left, the clinic system box in the middle, and secondary actors on the right. Booking includes finding a free slot, may be extended by the waitlist, and asks the SMS service to send a reminder. Paying a bill uses the payment gateway. Staff inherit the schedule view. Anything not inside the box, like insurance claims or lab reports, is now clearly out of scope.
Diagram to code
The diagram turns into code very directly. Each oval that an actor touches becomes one public method on a service interface, here called ClinicAppointments. The included use case becomes a private helper that is always called. The extend becomes a branch that runs only when the day is full. Each association line becomes a permission: before doing the work, the method checks that the caller's role is allowed this use case.
interface ClinicAppointments {
String bookAppointment(Actor by, ...);
String cancelAppointment(Actor by, ...);
String checkIn(Actor by, String apptId);
String recordConsultation(Actor by, ...);
String payBill(Actor by, ...);
List<String> viewSchedule(Actor by);
}
// include: a private helper, always called
// extend: a branch taken only if full Actors as roles
Actors become roles. Each role lists the use cases it is connected to. The receptionist and the doctor name clinic staff as their parent, so the can method checks the role's own list first, then asks the parent. That is actor generalization in about ten lines. Because the permissions come straight from the association lines, the code and the diagram stay in step: add a line, add one entry.
enum Role {
PATIENT(null, BOOK, CANCEL, PAY_BILL),
CLINIC_STAFF(null, VIEW_SCHEDULE),
RECEPTIONIST(CLINIC_STAFF, BOOK,
CANCEL, CHECK_IN),
DOCTOR(CLINIC_STAFF, RECORD_CONSULT);
boolean can(UseCase u) {
return own.contains(u)
|| (parent != null && parent.can(u));
}
} Run it
Watch the demo run one main goal per actor. The patient books the ten o'clock slot, and the SMS service sends a reminder for appointment A one. The receptionist checks the patient in. The doctor records the consultation notes. The patient pays five hundred rupees, and the gateway charges the card. Finally the patient tries to record consultation notes, and the service refuses, because there is no line between patient and that use case.
In the interview
In a design interview, use this diagram in the first few minutes to fix the scope. List the actors, including outside systems. For each, name two or three goals. Draw the box and say out loud what is outside it, like insurance and lab reports. Ask the interviewer to confirm. Then pick the one or two goals that matter most and design those in depth. The diagram becomes your contract for the rest of the round.
- 1 list the actors, including
- outside systems
- 2 for each, two or three goals
- 3 draw the box; say out loud
- what is OUT of scope
- 4 mark include and extend only
- if they shape the design
- 5 pick ONE goal to design
- in depth
- keep it to about 8 ovals
Common mistakes
Watch for the usual mistakes. The biggest one is drawing steps instead of goals, like click save or validate form. Another is joining ovals with arrows to show order. A use case diagram has no order; that belongs in a sequence or activity diagram. Do not put include on everything, do not make your own database an actor, and do not forget outside systems like payment. Name actors by role, and keep the diagram small.
- steps as ovals: Click save,
- Validate form, Open page
- arrows between ovals to show
- order (that is a flowchart)
- include on everything
- the database as an actor
- forgetting outside systems
- actors named after people
- instead of roles
- 40 ovals and no scope