Tuesday 15 August 2023

Namespaces in PlantUML Class Diagrams

In my previous posts, I explained about packages. But packages has a limitation, where the name of a class is unique across all the packages. We can overcome this limitation using namespaces.

 

A fully qualified class is automatically put into the namespace.  For example the class ‘java.lang.Double’ is put into the package ‘java.lang’.

 

namespace.txt

@startuml
	scale 600 width

	namespace java.lang #00FFFF {
		class System
		class Boolean
		class Object
	}

	class java.lang.Double
	class java.lang.Integer

	namespace java.util #gold{
		class Set
	}

	java.lang.Object <|-- java.util.Set

@enduml

 


Default namespace

Classes that are not part of any namespace are put into default namespace and accessed with a dot (.)

 

defaultNamespace.txt

@startuml

	class MyBaseClass

	namespace java.lang #00FFFF {
		class System
		class Boolean
		class Object
	}

	class java.lang.Double
	class java.lang.Integer

	namespace java.util #gold{
		class Set
	}

	java.lang.Object <|-- java.util.Set
	.MyBaseClass <|-- java.lang.Double

@enduml

Above snippet generate below diagram.




 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment