Singleton Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) singleton design pattern. By reading this tutorial, you will know how to develop a model for the signletion pattern, and how to apply it in practice.

September 30, 2009
Views: 77,109
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Singleton.
    New diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Singleton.
    New singleton class
  4. Right-click on the Singleton class and select Add > Attribute from the popup menu.
    Add attribute
  5. Name the attribute `instance` and set its type to `Singleton`.
    Instance attribute
  6. The `instance` attribute needs to be static. Right-click on the attribute and select Model Element Properties > Scope > Classifier from the popup menu.
    Set attribute to static
  7. Create a constructor for the Singleton class. Right-click on Singleton and select Add > Operation from the popup menu.
    Add constructor
  8. Name the operation `Singleton`, which is the same as the class name. Change the visibility from `+` (public) to `-` (private) in front of the operation name to indicate that this is a private constructor.
    Name constructor
  9. Right-click on Singleton and select Add > Operation from the popup menu again.
  10. Name the operation `getInstance` and set its return type to `Singleton`.
    Create getInstance operation
  11. The `getInstance` operation needs to be static. Right-click on the operation and select Model Element Properties > Scope > Classifier from the popup menu.
    Set operation to static
  12. In practice, there may be operations for accessing data in the Singleton class. To represent this, stereotype the Singleton class as `PTN Members Creatable`. Right-click on the Singleton class and select Stereotypes > Stereotypes... from the popup menu.
    Stereotype singleton class
  13. In the class specification dialog box, select PTN Members Creatable and click > to assign it. Click OK to confirm.
    Select PTN Members Creatable stereotype
    At this point, the diagram should look like this:
    Singleton diagram

Defining the Pattern

  1. Right-click on the Singleton class and select Define Design Pattern... from the popup menu.
    Define design pattern
  2. In the Define Design Pattern dialog box, specify the pattern name as Singleton. Keep the file name as is. Click OK to proceed.
    Set pattern name

Applying a Design Pattern to a Class Diagram

In this section, we are going to apply the singleton pattern in modeling a class registry.

  1. Create a new project named Class Registry.
  2. Create a class diagram named The Registry.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    Apply design pattern
  4. In the Design Pattern dialog box, select Singleton from the list of patterns.
    Select Singleton pattern
  5. Click on Singleton in the overview.
    Select Singleton in preview
  6. Rename the Singleton class and its constructor to ClassRegistry in the bottom pane.
    Rename Singleton
  7. We need to add an attribute for holding the classes that users register. Click the + button and select New Attribute... from the popup menu.
    New attribute
  8. In the Attribute Specification dialog, enter `classMapping` as the attribute name and `Map` as the type.
  9. We need to add operations for registering a class and retrieving a class by type. Click the + button and select New Operation... from the popup menu.
    new oper
  10. In the Operation Specification dialog box, enter `registerClass` as the operation name.
  11. Open the Parameters tab.
    Open Parameters tab
  12. Click Add... at the bottom of the specification dialog box.
  13. In the Parameter Specification dialog box, enter `name` as the parameter name and set `String` as the type. Click OK.
  14. Repeat the previous two steps to add a parameter named `regClass` and set its type to `Class`. Click OK to confirm.
    Parameters
  15. Click the + button again and select New Operation... from the popup menu.
  16. In the Operation Specification dialog box, enter `getClass` as the name and set `Class` as the return type.
    Pattern ready
  17. Click OK to apply the pattern to the diagram. This is the result:
    Pattern applied