Class diagram: Can I use links to members of classes in different namespaces?

0 votes
asked May 20 in Question / help by Piotr

I want to show a way to translate an object in one namespace into a similar object in another, For this, I'd like to be able to link specific class members in both namespaces.

@startuml
hide empty members

namespace NS1 {
  class DeviceState {
    + id_device
  }
}

namespace NS2 {
  class DeviceStatus {
    + device_id
  }
}

NS1.DeviceState::id_device -- NS2.DeviceStatus::device_id
@enduml

The code above creates additional boxes in both namespaces and links them. I want the link to go from the mentioned member in one class to the respective member in the other class. How can I do it?

Maybe I should switch to packages instead of namespaces? Will it help?

1 Answer

0 votes
answered May 20 by The-Lu (8,480 points)
 
Best answer

Hello Piotr,

If we switch to packages, and if for the same class name 'DeviceState' on different package we add an 'as' parameter: 'DS1' and 'DS2', we obtain:

@startuml
hide empty members

    package NS1 {
      class "DeviceState" as DS1 {
        + id_device
      }
    }

    package NS2 {
      class DeviceStatus {
        + device_id
      }
      class "DeviceState"  as DS2 {
        + id_device
      }
    }

DS1::id_device -- DeviceStatus::device_id  
DS1::id_device -- DS2::id_device
@enduml

PlantUML diagram
[Click to see on PlantUML server]

If that can help,
Regards,
Th.

commented May 21 by Piotr
Thanks, Th. I'd rather use namespaces, as there are a number of similar classes on both sides of the translation, many of them named identically.

But your solution works, and that's what counts. I have also tested that your solution works for links between enum values as well, so I can work with that.

Thanks for the prompt and valuable response,

Piotr.
...