Ejemplos de diagramas de clases UML
Dos dominios modelados en serio, con un motivo para cada línea: una biblioteca que necesita herencia y una cesta que necesita composición.
6 min de lecturaUML 2.5.13 de 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.
04Preguntas frecuentes
¿Cuál es un buen ejemplo de diagrama de clases UML?
Uno lo bastante pequeño para leerse de un vistazo y lo bastante completo como para discutirlo. Una biblioteca con un LibraryItem abstracto y dos subtipos muestra herencia; una cesta compuesta por sus líneas muestra composición. Entre los dos cubren cada tipo de relación que vas a dibujar.
¿Debe un diagrama de clases mostrar todas las clases?
No. Un diagrama es una vista de un modelo, no el modelo, y una vista que contiene todo no responde a nada. Dibuja las clases de las que trata la pregunta actual y deja las otras doscientas en el modelo, donde una herramienta seguirá encontrándolas.
¿Cuántas clases caben en un diagrama de clases?
Entre cinco y unas doce. Por debajo de cinco normalmente aún no se ha dicho lo suficiente para que valga la pena dibujarlo; por encima de doce las relaciones empiezan a cruzarse y el lector gasta su atención siguiendo líneas en lugar de entendiendo el dominio.
Lecturas relacionadas