Search code examples
umlplantuml

PlantUml: order classes around an interface


I am trying to create UML diagram with plantuml, this is my diagram:

@startuml
'https://plantuml.com/component-diagram


together {
class Rice {

}
class Egg extends IIngredient{

}
class Avocado extends IIngredient{

}
IIngredient <|-- Rice
}

abstract class IIngredient {
  @protected
  List<String>? allergens;
  @protected
  String? name;
  @protected
  int? price;

  String? getAllergens()
  String? getName()
  int? getPrice()
}

class Salmon {

}
class Tuna {

}
IIngredient <|-- Salmon
IIngredient <|-- Tuna

class Sesame extends IIngredient{

}

class Crisp extends IIngredient {

}
@enduml

the result is :

enter image description here

I want to IIngredient is center and other classes around it like this:

something like this:

enter image description here


Solution

  • Here's my take.

    1. Define your interface at the top before you link to it.
    2. Keep a simple usage. Using implements or extends means vertical alignment for inheritance. Using -r-|> forces the inheritance link in a direction (l, r, u, d) as mentioned by @Christophe.
    3. Horizontal layout is not as intelligent as vertical. In my answer here, I used only one class with r and l relationship, as more than one will result in links that curve around. The multiple u or d flow nicely.
    4. I am not sure why you used together, but you can achieve it by grouping classes either as u or d on the links.
    5. I also re-formatted your IIngredient class. Maybe you want it to be an interface? In that case, it's interface IIngredient ... and you should change your relationships to be .r.|> to show dashed lines (for implements).
    @startuml
    
    hide empty members
    
    abstract class IIngredient {
      ' # means protected visibility
      ' UML syntax name : type for attributes
      ' no "optional" like "?" in typescript
      #allergens:List<String>
      #name:String
      #price:int
      getAllergens():String
      getName():String
      getPrice():int
    }
    
    Egg -d-|> IIngredient
    Avocado -d-|> IIngredient
    Rice -d-|> IIngredient
    Salmon -u-|> IIngredient
    Tuna -u-|> IIngredient 
    Sesame -l-|> IIngredient
    Crisp -r-|> IIngredient
    @enduml
    

    enter image description here