Příklady UML diagramů tříd
Dvě domény pořádně namodelované a zdůvodnění každé čáry na nich - knihovna, která potřebuje dědičnost, a košík, který potřebuje kompozici.
6 min čteníUML 2.5.13 z 28
01A library, where inheritance earns its place
The diagram above is worth reading relationship by relationship. Book and DVD generalize LibraryItem, which is drawn as a hollow triangle pointing at the parent - and the reason to draw it that way rather than as two unrelated classes with duplicated attributes is the association below it. Loan points at exactly one LibraryItem, and because it points at the abstract parent it can lend either subtype without knowing which.
That is the whole test for inheritance in a domain model: not "do these two things feel similar", but is there somewhere in the model that wants to hold either one without caring which. If nothing does, two separate classes are the smaller and more honest answer.
Note the multiplicities on the Member-to-Loan association: 1 to 0..*. A member with no loans is a member; a loan with no member is a bug. That asymmetry is a real fact about the domain and it is stated on the line rather than left to a comment. The class diagram article covers how each of those symbols is drawn.
02A basket, where composition earns its place
This one contains no inheritance at all, and that is the point of showing it beside the library. What it has instead is a composition and an interface, and each is there for a reason the other diagram had no use for.
The filled diamond between Basket and BasketLine is a claim: delete the basket and the lines go with it, because a basket line is meaningless on its own. Compare it with the plain association from BasketLine to Product, where the product obviously outlives the line - the same shape of relationship, two different lifetimes, and the notation distinguishes them.
PricingRule is the extension point. Two realizations are drawn because one realization is not an abstraction, it is a class with an extra step - and the moment there are two, adding a third promotion is a new class rather than a new branch in an existing method.
03How to read either one in thirty seconds
Both diagrams reward the same reading order, and it is not left to right. Start with the multiplicities, because they are the decisions: they say what the system permits and they are the hardest thing to change later. Then read the diamonds, which say what gets deleted with what. Then the triangles, which say what can stand in for what. Attributes last, and only the ones you doubt.
A diagram that survives that reading is worth keeping. One where the multiplicities are blank is not finished, however many attributes it lists - and that is by far the most common state to find a class diagram in.
In one line each
- 01Draw inheritance when something in the model holds the parent type without caring which subtype it has.
- 02Use composition when the part is deleted with the whole, and plain association otherwise.
- 03An interface with one realization is a class with extra steps; two makes it an extension point.
- 04Multiplicities are the decisions on the diagram. Blank ones mean the diagram is unfinished.
- 05Five to twelve classes per diagram. A view holding everything answers nothing.
Next: how to draw one from scratch, and the symbol reference for checking a mark you are unsure of.
04Časté dotazy
Jaký je dobrý příklad UML diagramu tříd?
Takový, který se dá přečíst jedním pohledem a je zároveň dost úplný na to, aby se o něm dalo diskutovat. Knihovna s abstraktní třídou LibraryItem a dvěma podtypy ukáže dědičnost; košík složený ze svých položek ukáže kompozici. Společně pokryjí každý druh vztahu, který budete kreslit.
Má diagram tříd zobrazit každou třídu systému?
Ne. Diagram je pohled na model, ne samotný model, a pohled, který obsahuje všechno, neodpovídá na nic. Nakreslete třídy, o kterých je aktuální otázka, a zbylých dvě stě nechte v modelu, kde je nástroj stále najde.
Kolik tříd patří na jeden diagram tříd?
Pět až zhruba dvanáct. Pod pět obvykle ještě není řečeno dost na to, aby se vyplatilo kreslit; nad dvanáct se vztahy začnou křížit a čtenář utrácí pozornost za sledování čar místo pochopení domény.
Související články