Archyno
UMLDiagramy štruktúry

Ako nakresliť UML diagram tried

Päť krokov v poradí, ktoré funguje: nájdite podstatné mená, nakreslite čiary, rozhodnite násobnosti, potom atribúty a nakoniec operácie - a skončite, keď diagram odpovedá na otázku.

7 min čítaniaUML 2.5.14 z 28

PatientAppointmentDoctorPrescription
After step two. Four nouns and three lines, no attributes - and already worth showing somebody.

01Steps one and two: the nouns, then the lines

Step one is to write down how the domain is described out loud and underline the nouns. "A patient books an appointment with a doctor, and the doctor may issue a prescription" gives four candidates immediately. Keep the ones with three properties: identity (two of them can be distinguished), state that changes over time, and a lifetime of their own. A noun failing all three - a colour, an amount, a status - is an attribute of something else.

Step two is one line per sentence you can say about the domain. Not per database join, and not per method call: a line means the two classes know about each other in the business sense. The diagram above is where that ends, and it is already worth putting in front of somebody who knows the domain, because the argument you want is about whether a prescription belongs to an appointment or to a patient - and that argument is available now, before any attribute has been typed.

02Step three: the multiplicities, before anything else

Every line gets a number at each end before a single attribute is added. This is the step people skip and the one that pays, because a multiplicity is a decision about what the system permits and an attribute is a detail that follows from it.

Read each end as a sentence and say it aloud. "One patient has zero or more appointments" - fine. "One appointment has exactly one patient" - fine, and worth checking, because a clinic that ever books a family slot has just told you otherwise. The three questions to ask at every end are: can it be zero, can it be more than one, and does that change on a bad day?

Then pick the relationship kind, and there are only two decisions worth agonising over. Is the part deleted with the whole - if yes, composition. Does something hold the parent type without caring which subtype - if yes, generalization. Everything else is a plain association, and the worked examples show both calls being made.

03Steps four and five: attributes, operations, and stopping

10..*0..*110..*Patient- id: UUID- name: String- dateOfBirth: DateAppointment- startsAt: Instant- duration: Minutes- status: Status+ book(): void+ cancel(r: Reason)Doctor- id: UUID- name: String- specialty: SpecialtyPrescription- drug: String- dose: String- issuedOn: Date
The same model at step five. Note the composition: a prescription does not outlive its appointment.

Attributes come fourth and each one has to have a source. If you cannot say where a value comes from - a form, another system, a calculation - it is a guess, and guesses are what make a model stop matching the thing it describes. Types are worth writing: startsAt: Instant settles an argument that startsAt: Date leaves open.

Operations come fifth and most classes get none. An operation belongs on a class when the behaviour genuinely needs that class's own state to work - cancel() needs the appointment's status, so it belongs. Anything that needs three other objects to do its job is a service, and putting it here is how a domain model quietly becomes a diagram of the code instead of a diagram of the domain.

Then stop. The finished diagram above has four classes and answers a question; adding Clinic, Room, Invoice and Insurer would make it more complete and less useful. If a second question needs answering, draw a second view over the same model - that is what views are for.

In one line each

  1. 01Nouns with identity, changing state and their own lifetime become classes. The rest are attributes.
  2. 02Draw the lines before the attributes, and show the ugly version - it is the one people correct.
  3. 03Decide multiplicities third, out loud, asking whether each end can be zero or more than one.
  4. 04Every attribute needs a source. Types settle arguments that names leave open.
  5. 05Stop when the question is answered, not when the system is fully drawn.

04Časté otázky

Ako sa rozhodnúť, čo sa stane triedou?

Vezmite podstatné mená z toho, ako sa doména opisuje nahlas, a nechajte tie, ktoré majú identitu, meniaci sa stav a vlastnú životnosť. Podstatné meno, ktoré je vždy len hodnotou niečoho iného, ako farba alebo suma, je atribút, nie trieda.

V akom poradí kresliť diagram tried?

Triedy, potom vzťahy, potom násobnosti, potom atribúty a nakoniec operácie. Atribúty sa zdajú byť samozrejmým začiatkom a sú tým najhorším, pretože práve ich najskôr zahodíte, keď vás vzťahy donútia prehodnotiť, čo kam patrí.

Kedy je diagram tried hotový?

Keď odpovedá na otázku, kvôli ktorej vznikol, a každý vzťah na ňom nesie násobnosť. Nie vtedy, keď sú na ňom všetky triedy systému, a nie vtedy, keď sú vymenované všetky atribúty - do oboch týchto stavov sa diagram dokáže dostať bez toho, aby na čokoľvek odpovedal.

ZdieľaťXLinkedIn
Všetky články: UML
Všetky články