Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • September 30, 2009
  • Views: 58,283
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Singleton.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as 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. Set its type as Singleton.
    instance attr
  6. The attribute instance need to be static. Right-click on the attribut and select Model Element Properties > Scope > Classifier from the popup menu.
    set attr static
  7. Create 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 follows the Singleton class's name. Change + to - 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.
  10. Name the operation getInstance, and make it return Singleton.
    create get instance
  11. The operation getInstance need to be static. Right-click on the operation and select Model Element Properties > Scope > Classifier from the popup menu.
    set oper 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
  13. In the class specification dialog box, select PTN Members Creatable and click > to assign it. Click OK to confirm.
    select ptn members creatable
    Up to now, the diagram should look like this:
    singleton diagram

Defining Pattern

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

Applying Design Pattern on Class Diagram

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

  1. Create a new project Class Registry.
  2. Create a class diagram 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 class
  5. Click on Singleton in the ovewview.
    select single in preview
  6. Rename the class Singleton, as well as the constructor to ClassRegistry at the bottom pane.
    rename singleton
  7. We need to add an attribute for holding the classes user register. Click on the + button and select New Attribute... from the popup menu.
    new attr
  8. In the Attribute Specification, enter classMapping as attribute name. Enter Map as type.
    set attr name type
  9. We need to add operations for registering class and retrieving class by type. Click on the + button and select New Operation... from the popup menu.
    new oper
  10. In the Operation Specification dialog box, enter registerClass as operation name.
  11. Open the Parameters tab.
    open params
  12. Click Add... at the bottom of specification dialog box.
  13. In the Parameter Specification dialog box, enter name as parameter name and set String as type.
    fill param spec
  14. Repeat steps 12 and 13 to add parameter regClass, and set Class as type. Click OK to confirm.
    params
  15. Click on the + button and select New Operation... from the popup menu.
  16. In the Operation Specification dialog box, enter getClass as name, and set Class as return type.
    pattern ready
  17. Click OK to apply the pattern to diagram. This is the result:
    pattern applied



Turn every software project into a successful one.

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK