Dessiner un diagramme de composants
Six étapes d'une page blanche à un diagramme que l'on peut remettre à une équipe, sur un petit système. L'ordre compte plus que la notation : les pièces, puis les promesses, puis les besoins, puis les vérifications qui repèrent une mauvaise frontière avant que quiconque construise dessus.
8 min de lectureUML 2.5.19 sur 23
01Step 1. List what could be replaced
Before drawing anything, write a list. Not of your modules, not of your folders - of the parts of the system that could plausibly be swapped for a different implementation: bought instead of built, rewritten by another team, or run by somebody else.
That question is the whole filter. A folder is not a component and neither is a class; a thing with a boundary somebody could hand to a supplier is. The reporting system in this article produced four: a dashboard, a scheduler that runs reports overnight, the report service itself, and an adapter that talks to the data warehouse.
02Step 2. Draw the boxes, and no arrows
Place the parts and resist connecting them. Drawing arrows now means drawing them between components, and a line from Dashboard straight to Report service says only "these two are involved somehow", which is the vagueness a component diagram exists to remove.
Layout is worth thirty seconds here: put consumers on one side and providers on the other. Every diagram in this series is drawn left to right, dependencies flowing one way, because a reader can then check the direction at a glance rather than by following each line.
03Step 3. Name what each part provides
For each box, ask what somebody outside it is allowed to rely on, and name that. IReports and IWarehouse here - one contract per capability, not one per consumer. Attach it with a realization: dashed line, hollow triangle, pointing at the interface.
This step is where the real design happens, and it is the step people skip. Naming a contract forces you to decide what is public, and the argument that follows - "is the CSV export part of IReports or not?" - is the argument worth having before two teams answer it differently in code.
04Step 4. Add what each part requires
Now the second half, and the half that carries the information: for each component, which contracts does it need? Draw a dependency - dashed line, open arrowhead - from the component to the interface it requires. The finished result is the diagram at the top of this article.
Two things become visible the moment those arrows land. The dashboard and the scheduler both point at IReports, so changing it is now visibly a change to two consumers. And the report service points at IWarehouse rather than at the adapter, so the adapter is replaceable without the service noticing - which is either what you intended or a discovery worth making now.
05Step 5. Run four checks
A component diagram is finished when it survives these. They take a minute and each one has caught a real problem more often than it has passed cleanly.
- Cover a box with your hand. What is left - the interfaces that were attached to it - is the specification of its replacement. If a replacement would need to know something that is not on screen, the boundary is in the wrong place.
- Follow the arrows for a cycle. Two components that require each other cannot be deployed, tested or replaced independently. See the last of the worked examples for what that looks like and how it gets broken.
- Check every component has at least one interface. A box with nothing attached is either not a component or not relevant to this diagram.
- Read the interface names aloud. If one is named after its current provider rather than its capability -
IMainframeBillingrather thanIBilling- the contract has the supplier baked into it, and the swap the diagram promises will not be as clean as it looks.
06Step 6. Delete what belongs on another diagram
Reach for it when
- Interfaces, and which component is on which side of each
- Stereotypes that change how a part is procured - «subsystem», «service», «library»
- Ports, when one component genuinely has two separate surfaces
- A note on any boundary that is contested, saying who decided it
Reach for something else when
- Servers, regions, containers and processes - deployment diagram
- Classes, fields and methods inside a component - class diagram
- The call sequence between components - sequence diagram
- Database tables and columns - ER diagram
The right-hand column is not pedantry about diagram kinds. Every item on it makes the picture bigger without answering the question this diagram was drawn for, and each one gives the diagram a second reason to go out of date: a component diagram survives a re-platforming untouched, and the same drawing with two AWS regions on it does not.
In one line each
- 01List what could be replaced. That list, not the folder tree, is your component list.
- 02Boxes first, no arrows - a line between two components states nothing checkable.
- 03Name the provided contracts before the required ones; that is where the design decisions are.
- 04Required interfaces carry the dependency information and are the half people omit.
- 05Cover any box: what remains must fully specify its replacement.
- 06Anything about machines, classes or call order belongs on a different diagram.
With the drawing done, the reference for every mark on it is in component diagram symbols, and the wider case for when this diagram kind is worth the effort at all is in the component diagram guide.
07Questions fréquentes
Par où commencer un diagramme de composants ?
Commencez par lister les parties du système que l'on pourrait plausiblement remplacer ou acheter plutôt que construire, et arrêtez-vous à neuf. C'est cette question, et non l'arborescence des dossiers, qui décide des boîtes à dessiner, et y répondre d'abord évite de produire une image du dépôt.
Que dessine-t-on en premier, les composants ou les interfaces ?
Les composants d'abord, mais seulement comme des boîtes sans flèches. Nommer les contrats est la partie difficile, et la faire ensuite permet de les nommer en sachant quelles pièces se tiennent de part et d'autre, au lieu d'inventer une interface puis de chercher quoi mettre derrière.
Quel niveau de détail pour un diagramme de composants ?
Assez de détail pour que quelqu'un puisse construire le remplaçant de n'importe quelle boîte à partir de ce qui l'entoure, et pas davantage. Les opérations et les paramètres relèvent de la définition d'interface ou du code : un diagramme qui liste des signatures est périmé la semaine suivante.
Comment savoir qu'un diagramme de composants est terminé ?
Quand chaque composant porte au moins une interface, qu'aucune flèche ne vise un composant au lieu d'un contrat, et que masquer n'importe quelle boîte laisse encore de quoi spécifier son remplaçant. Si les trois tiennent, le diagramme énonce quelque chose de vérifiable et vous pouvez vous arrêter.
À lire aussi