Search code examples
plantuml

Reuse of component in PlantUML Component Diagram


I'm trying to draw a component UML diagram using PlantUML.

I want to declare a component C and reuse it in several nodes.

I tried with a code like the following

@startuml

cloud CMS #darkgreen {
component NMProxy #lightgreen 
}

component C #orange
node GW1 #brown {
C 
}

node GW2 #brown {
C
}

node GW3 #brown {
C
}

GW1 -up-> NMProxy
GW2 -down-> NMProxy
GW3 -down-> NMProxy

@enduml

But the parser gives me an error at the first usage of the component C in node GW1.

Does a method exist to declare a component before its usage and then reuse it using its name?


Solution

  • I think the reason this isn't working is because you can't reuse the same component multiple times (because it's a definition, like a class).

    The best UML-like way to do this is based on this answer: https://softwareengineering.stackexchange.com/a/365901/51948

    That is, each of your "instances" of C are going to implement the same interface C. I don't think it makes sense in a component diagram to reuse the same definition of a component as sub-components.

    So, here's what it would look like in a deployment diagram:

    @startuml
    
    interface C #orange
    
    cloud CMS #green {
    component NMProxy #lightgreen 
    }
    
    node GW1 #brown {
      component C1
    }
    
    node GW2 #brown {
      component C2
    }
    
    node GW3 #brown {
      component C3
    }
    
    C1 -d-( C
    C2 -d-( C
    C3 -d-( C
    
    GW1 -u-> NMProxy
    GW2 -u-> NMProxy
    GW3 -u-> NMProxy
    
    @enduml
    

    Deployment diagram

    I realize this is not so nice to look at, but it describes more precisely using UML.

    If you don't care about ambiguity, here's a solution that hides the fact that there are three separate components with the same name/color. Unfortunately, I don't think it's possible to define that in one place and re-use it.

    @startuml
    
    cloud CMS #green {
      component NMProxy #lightgreen 
    }
    
    node GW1 #brown {
      component "C" as C1 #orange
    }
    
    node GW2 #brown {
      component "C" as C2 #orange
    }
    
    node GW3 #brown {
      component "C" as C3 #orange
    }
    
    GW1 -u-> NMProxy
    GW2 -d-> NMProxy
    GW3 -d-> NMProxy
    
    @enduml
    

    Component diagram reusing same component (more or less)