如何让PlantUML中IIngredient抽象类居中,子类环绕排布?
调整PlantUML类图布局:让抽象类居中,子类环绕
当前使用的PlantUML代码
@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
当前生成的类图效果

期望的布局效果

修改后的代码(实现居中环绕布局)
@startuml ' 设置布局为环绕中心类 circle IIngredient abstract class IIngredient { @protected List<String>? allergens; @protected String? name; @protected int? price; String? getAllergens() String? getName() int? getPrice() } ' 统一继承关系写法,去掉不必要的together块 class Rice extends IIngredient {} class Egg extends IIngredient {} class Avocado extends IIngredient {} class Salmon extends IIngredient {} class Tuna extends IIngredient {} class Sesame extends IIngredient {} class Crisp extends IIngredient {} @enduml
关键调整说明
- 移除原代码中的
together块,避免强制类聚集,影响自动布局逻辑 - 添加
circle IIngredient指令,让所有继承该抽象类的子类自动环绕在其周围 - 统一使用
extends关键字定义继承关系,简化代码结构 - 清理类定义中的冗余空行,保持代码整洁
内容的提问来源于stack exchange,提问作者Cyrus the Great
相关产品推荐
相关产品推荐

