umlcomponent-diagram

How can two components contain and use the third one?


I have two components A and B. I want the model to show that they both <<include>> and <<use>> the third component. Which picture is correct, the top or bottom one?

enter image description here


Solution

  • Often, when you have two possibilities in UML, both are correct. But not here. I am afraid, both are bad.

    According to the current standard 2.5 :

    A Usage is a Dependency in which one NamedElement requires another NamedElement (or set of NamedElements) for its full implementation or operation. The Usage does not specify how the client uses the supplier other than the fact that the supplier is used by the definition or implementation of the client.

    So, in UML <<use>> means "must have for some use". And notice, it is absolutely independent on the containment/inclusion relationship. The object we are depending on can be even contained, for it is not said we need it for creation of the dependent object.

    We can use a component that belong to anywhere. But in the including (top) diagram we can't show the usage. So, it won't pass.

    As for the second sort of relationship, including, there are greater problems here.

    The standard UML component diagram has no "include" stereotype. But it is allowed to use elements of other diagrams.

    But the component is not a class, it is an object. So, your component C simply cannot belong to both A and B. The other diagram fails, too. And if you want to have two different objects/components based on the same class or package, absolutely different diagram appears.

    correct comp diagram

    So, either A or B has its own instance of C and uses this instance. It is clean, without peeking into the other component.

    If you use the tool that has not that containment connection, use the "include" association from the use case diagram.

    I have also a strong suspicion that you could need not Component, but some different diagram, for your wish to put one box into different ones is unnatural.