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.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Singleton.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it Singleton.

- Right-click on the Singleton class and select Add > Attribute from the popup menu.

- Name the attribute `instance` and set its type to `Singleton`.

- The `instance` attribute needs to be static. Right-click on the attribute and select Model Element Properties > Scope > Classifier from the popup menu.

- Create a constructor for the Singleton class. Right-click on Singleton and select Add > Operation from the popup menu.

- 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.

- Right-click on Singleton and select Add > Operation from the popup menu again.
- Name the operation `getInstance` and set its return type to `Singleton`.

- The `getInstance` operation needs to be static. Right-click on the operation and select Model Element Properties > Scope > Classifier from the popup menu.

- 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.

-
In the class specification dialog box, select PTN Members Creatable and click > to assign it. Click OK to confirm.
At this point, the diagram should look like this:
Defining the Pattern
- Right-click on the Singleton class and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Singleton. Keep the file name as is. Click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we are going to apply the singleton pattern in modeling a class registry.
- Create a new project named Class Registry.
- Create a class diagram named The Registry.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Singleton from the list of patterns.

- Click on Singleton in the overview.

- Rename the Singleton class and its constructor to ClassRegistry in the bottom pane.

- We need to add an attribute for holding the classes that users register. Click the + button and select New Attribute... from the popup menu.

- In the Attribute Specification dialog, enter `classMapping` as the attribute name and `Map` as the type.
- 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.

- In the Operation Specification dialog box, enter `registerClass` as the operation name.
- Open the Parameters tab.

- Click Add... at the bottom of the specification dialog box.
- In the Parameter Specification dialog box, enter `name` as the parameter name and set `String` as the type. Click OK.
- Repeat the previous two steps to add a parameter named `regClass` and set its type to `Class`. Click OK to confirm.

- Click the + button again and select New Operation... from the popup menu.
- In the Operation Specification dialog box, enter `getClass` as the name and set `Class` as the return type.

- Click OK to apply the pattern to the diagram. This is the result:
