Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Singleton.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Singleton.
- Right-click on the Singleton class and select Add > Attribute from the popup menu.
- Name the attribute instance. Set its type as Singleton.
- The attribute instance need to be static. Right-click on the attribut and select Model Element Properties > Scope > Classifier from the popup menu.
- Create constructor for the Singleton class. Right-click on Singleton and select Add > Operation from the popup menu.
- 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.
- Right-click on Singleton and select Add > Operation from the popup menu.
- Name the operation getInstance, and make it return Singleton.
- The operation getInstance need 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.
Up to now, the diagram should look like this:
Defining 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 Singleton. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the singleton pattern in modeling a class registry.
- Create a new project Class Registry.
- Create a class diagram 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 ovewview.
- Rename the class Singleton, as well as the constructor to ClassRegistry at the bottom pane.
- We need to add an attribute for holding the classes user register. Click on the + button and select New Attribute... from the popup menu.
- In the Attribute Specification, enter classMapping as attribute name. Enter Map as type.
- 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.
- In the Operation Specification dialog box, enter registerClass as operation name.
- Open the Parameters tab.
- Click Add... at the bottom of specification dialog box.
- In the Parameter Specification dialog box, enter name as parameter name and set String as type.
- Repeat steps 12 and 13 to add parameter regClass, and set Class as type. Click OK to confirm.
- Click on the + button and select New Operation... from the popup menu.
- In the Operation Specification dialog box, enter getClass as name, and set Class as return type.
- Click OK to apply the pattern to diagram. This is the result: