How would I represent the following C# Singleton in a UML class diagram?

Answered How would I represent the following C# Singleton in a UML class diagram?

  • Wednesday, May 05, 2010 12:10 AM
     
      Has Code

    I  would like to represent a Singleton pattern using automatic properties and a static constructor in Visual Studio 2010 UML. Does anyone know how this would be represented or have a link that could help?

    public sealed class Singleton
    {
         public static Singleton Instance { get; private set; }
         static Singleton() { Instance = new Singleton(); } Singleton() { }
         public void Test() { Console.WriteLine("Successfully created the Singleton."); }
    }
    

    I tried reverse engineering it and it generated the following Visio 2010 Diagram of this form:

    -----------------------------------
    |            Singleton                |
    -----------------------------------
    |                                             |
    -----------------------------------
    | +Instance() : Singleton    |
    | -Singleton()                       |
    | -Singleton()                       |
    | +Test()                               |
    -----------------------------------

    This does not appear to fully represent the class as written, so I am hoping there is some kind of nomenclature to accomplish the class visualization.

     

     

All Replies

  • Monday, May 10, 2010 5:29 AM
     
     

    I believe that the normal way to identify a class in the class diagram as a singleton would be to use a stereotype in this case <<singleton>> (you can view the following site for some more information:

    Here is the correct link!

    http://users.skynet.be/averkeyn/computer/uml.html

    This was the bad link:

    http://www.certiport.com/Portal/desktopdefault.aspx?page=common/pagelibrary/Contact_Us.html)

     

  • Monday, May 10, 2010 4:19 PM
     
     
    I believe that the normal way to identify a class in the class diagram as a singleton would be to use a stereotype in this case <<singleton>> (you can view the following site for some more information: http://www.certiport.com/Portal/desktopdefault.aspx?page=common/pagelibrary/Contact_Us.html)
    Not sure why you gave that link. It doesn't have any useful information and appears to be nothing more than a solicitation to purchase online technical exams. What does that have to do with my question?
  • Monday, May 10, 2010 5:34 PM
     
     Answered

    Sorry - I must have not gottent the right link copied, and didn't verify before I posted...

    Here is the link I intended to post:

    http://users.skynet.be/averkeyn/computer/uml.html

    You should find it relevant! - and again I'm very sorry! (I'm also going to edit my original answer :-))

  • Monday, May 10, 2010 8:01 PM
     
     
    Much better... however, I am beginning to think that it is not possible to fully represent C# classes in UML. For example, things like automatic parameters and static constructors shown in my original example of an elegant, compact, thread-safe singleton cannot be accurately represented as far as I can tell. In my case, the static constructor with an initialize and the public constructor used to guarantee the singleton cannot be instantiated outside of the class both look identical in the UML.