单例可以画状态机图吗?

Can a state machine diagram be drawn for a singleton?

我有一个执行多项任务的单例 class:

我想为 class 绘制状态机图,但想知道为单例绘制状态机图是否正确。

当然可以。一个不必与另一个做任何事情。单例意味着只有一个实例。现在状态机可以根据您想要实现的目标来尊重或忽略这一事实。

为什么单例实例应该与任何其他实例不同?单例模式只是将构造函数设为私有,因此只能有一个实例,通过 getInstance() 操作获得。

您应该考虑在 UML 中将 class 建模为 "active class" 以声明 class 具有 "never" 结束的自主行为。如 UML 2.5 规范中所述:

An active object is an object that, as a direct consequence of its creation, commences to execute its classifierBehavior, and does not cease until either the complete Behavior is executed or the object is terminated by some external object. (This is sometimes referred to as “the object having its own thread of control.”) The points at which an active object responds to communications from other objects is determined solely by the Behavior of the active object and not by the invoking object. If the classifierBehavior of an active object completes, the object is terminated.