PlantUML - 如何将私有方法表示为序列图中的嵌套生命线

PlantUML - How to Represent Private Methods as Nested Lifelines inside a Sequence Diagram

我正在使用 PlantUML 创建一个序列图,它描述了一个约会应用程序点击一个 RestfulController(它使用一个服务 class 来处理数据)。

我想要做的是通过序列图表示服务 class 的内部私有方法。

注意:这是伪代码,请不要考虑语义。

class DatingApp {
    
    public void hitExternalApi() {
    }
}

class DatingRestController {
    
    @Autowired
    public void DatingService;

    @GetMethod
    public Object processService() {
        return DatingService.findProfile();
    }
}

class DatingService {

    public Object findProfile() {
        Object retValue = new Object(null, null);
        var variable1 = doSomething();
        var varable2 = doSomethingElse();
        return retValue(variable1, variable2);
    }

    private String doSomething() {
    }

    private String doSomethingElse() {
    }
}

PlantUML DSL 文件:

@startuml
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: processService()
DatingService -> DatingService: findProfile()
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
DatingService -> DatingRestController: sent retValue
DatingRestController -> DatingApp: Send JSON
@enduml

我的初始传球:


如您所见,这看起来像是 DateService class 正在调用 process() 方法,然后随后调用 doSomething()doSomethingElse() 方法。

如何表示 doSomething()doSomethingElse() 方法是从 findProfile() 生命线内部调用的,而不是看起来像外部 public 调用?

您可以使用 activatedeactivate 来显示哪个对象处于活动状态并使用 return 函数调用。

这是一个例子:

@startuml
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: process()
activate DatingService
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
DatingService --> DatingRestController
deactivate DatingService
@enduml

uml 序列图没有显示 private/public 方法调用的规则,但没有什么能阻止您添加自己的规则,并带有说明如何阅读图形的图例。 例如,您可以使用颜色来表示私人规则

@startuml
legend top left
  <color blue> Blue calls</color> denote public method calls
  <color red> Red calls</color> denote private method calls
endlegend
DatingApp -[#blue]> DatingRestController: hitExternalApi()
DatingRestController -[#blue]> DatingService: process()
activate DatingService 
DatingService -[#red]> DatingService: doSomething()
DatingService -[#red]> DatingService: doSomethingElse()
DatingService -[#blue]-> DatingRestController
deactivate DatingService 
@enduml

颜色也可以用在激活线上,但序列描述有点复杂:

@startuml
legend top left
  <color blue> Blue activation </color> denote public method calls
  <color red> Red activation</color> denote private method calls
endlegend
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: process()
activate DatingService #blue

DatingService -> DatingService: doSomething()
activate DatingService #red
deactivate DatingService

DatingService -> DatingService: doSomethingElse()
activate DatingService #red
deactivate DatingService

DatingService --> DatingRestController


deactivate DatingService 
@enduml

因此,经过反复试验,我查看了 PlantUML 文档并发现真正的问题是如何将私有方法调用表示/表示为嵌套生命线。

这是我的解决方案:

@startuml
skinparam Shadowing false
title __Dating API Sequence Diagram__\n
caption \nVersion 1.0 - 6/26/2020 (Draft)\n
autonumber
activate DatingApp
DatingApp -> DatingRestController: hitExternalApi()
activate DatingRestController
DatingRestController -> DatingService: processService()
activate DatingService
DatingService -> DatingService: findProfile()
activate DatingService #90EE90
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
deactivate DatingService
DatingService -> DatingRestController: return retValue
DatingRestController -> DatingApp: jsonPayload
deactivate DatingRestController
deactivate DatingApp
legend bottom right
Legend
|=Color |= Name |= Type |= Lifeline |
|<back:#FFFFFF>           </back>| DatingApp.hitExternalApi() | method | default |
|<back:#FFFFFF>           </back>| DatingRestController.processService() | method | default |
|<back:#FFFFFF>           </back>| DatingService.findProfile | method | default |
|<back:#90EE90>           </back>| DatingService.doSomething() | method | nested |
|<back:#90EE90>           </back>| DatingService.doSomethingElse() | method | nested |
endlegend
@enduml

这是从 IntelliJ IDEA 生成的序列图:


doSomethingElse() 方法是否仍然可以指向绿色嵌套管道,或者它的箭头指向包含调用 findProfile() 方法的默认白色生命线是否正确?


PlantUML 是一个很棒的工具,我期待着越来越好地使用它并帮助这里的其他人解决他们的问题!