Singleton In Uml For Java

Here's a non-qualified association I don't know how to draw. Take the singleton pattern as usually manifested in JDK 1.1. Usually this is done by either (1) a static variable in the object which defines main(String[] args) or (2) a variable inside main itself. In the first case, this is an association with "ownership" semantics between two classes. In the second case, it's a dependency (dashed arrow) with ownership semantics. In either case, I don't know what the "correct" diagram is (aside from attaching a note to the association explaining things textually).

WilliamGrosso


My understanding of SingletonPattern is that a singleton class is one where all the references to objects of that class refer to the same object. That is, it is a fact about a class itself, not about the clients of that class, nor about the program within which the class is used.

Implementing that fact via a reference in a class defining main() makes some stronger statement about the lifetime of that one instance (for a particular execution, many Java systems have a lot of main()s around). For Java this stronger statement is required in some form (see JavaSingleton).

Anyway, to show in UML that a class is a Singleton, why not say this (in ObjectConstraintLanguage):

       ASingletonClass.allInstances->size = 1 

or perhaps

       ASingletonClass.allInstances->size <= 1

depending.

More generally, create a stereotype <<Singleton>> with a constraint on it that says the same, then apply that stereotype to your class.

How you then go about implementing that stereotype in some language is not, IMHO an appropriate thing to put in a model, unless that model is explicitly an ImplementationModel?, of the kind discussed in SyntropyMethodology and elsewhere. But then the UML isn't really strong enough to do that properly.

Maybe a Booch expert could tell us if William can do what he wants using that notation, since it is more "design-y", while the UML has more of an OMT "analysis-y" feel.

--KeithBraithwaite


Why did I pick on singleton ? The answer is "It's a test case."

Singleton is a conceptually simple pattern ("Reify a global") and yet, when you poke at it, it's more complicated to capture in a model than I would have expected (especially when you consider the Java 2 notion of implementing singletons via ClassLoaders?).

The original question (in UmlIssuesWithQualifiedAssociation) was about the semantics of qualified associations (and the associated fringe cases). My best guess about where the semantics would get tricky is involves object ownership and lifecycle issues (as an aside: a caching system, where the cache is a singleton, seems to fit the 1 to * model as well).

WilliamGrosso


In my experience, I would say that about 75% of the time when I implement a Singleton, I wind up extending the system to have several DistinguishedInstance?s of which the Singleton is one. This leads me to think that if one were into modeling, the thing to model is Distinguished Instances. --RonJeffries


See also TextAndGlyphsInTheUml


EditText of this page (last edited October 8, 2007) or FindPage with title or text search