How do I mark a class as a singleton in UML?

拈花ヽ惹草 提交于 2019-11-30 21:55:45

问题


I have an UML diagram on which I want to mark some classes as singletons (because they are). How do I do that? Do I invent a new stereotype or do I just add a comment or are there some existing means to do that?


回答1:


Craig Larman's "Applying UML and Patterns" book (3rd edition) gives the following notation for a sequence diagram:

Note that it implies that before calling doA the :Register object manages to get access to the singleton via the standard way, e.g., getInstance(). This makes the diagram more agile.

For a class diagram:




回答2:


"What will most clearly represent the concept to your reader."

There is a reasonable example of the basic appearance over on Wikipedia. It shows a private constructor and a public getInstance() method that returns an object of type Singleton. I'd also find a stereotype to be a perfectly reasonable way to make it explicit without requiring your reader to parse the details of the class object.




回答3:


To specify that a class is a singleton, you can write a constraint in between braces: { number of instances = 1 }. This constraint should be put in a constraints compartment in the class rectangle.

The UML 2.5 specification, §7.6.4 defines the notation for constraints in general and §9.2.4 specifies how to show the constraints of a classifier:

If a Classifier owns Constraints, a conforming tool may implement a compartment to show the owned Constraints listed within a separate compartment of the owning Classifier’s rectangle. The name of this optional compartment is “constraints.”

Alternatively, you could invent your own stereotype ≪singleton≫.



来源:https://stackoverflow.com/questions/8386785/how-do-i-mark-a-class-as-a-singleton-in-uml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!