gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:24:31 26 4
gpt4 key购买 nike

我使用 PlantUML 创建一个序列图,该图描述了一个约会应用程序命中 RestfulController(它使用 Service 类来处理数据)。

我想要做的是通过序列图来表示 Service 类的内部私有(private)方法。

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

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

我的初始通过:

enter image description here


正如您所看到的,这看起来像 DateService 类正在调用 process() 方法,然后调用 doSomething()doSomethingElse() 方法。

我如何表示从 findProfile() 生命线中调用 doSomething()doSomethingElse() 方法,而不是看起来像外部公开电话?

最佳答案

您可以使用activatedeactivate来显示哪个对象处于 Activity 状态并使用返回函数调用。

这是一个例子:

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

enter image description here

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

@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

enter image description here

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

@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

enter image description here

关于java - PlantUML - 如何将私有(private)方法表示为序列图中的嵌套生命线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62602005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com