Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Adapter.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Client.
- Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Target.
- Right click on Target, and select Model Element Properties > Abstract to set it as abstract.
- Right click on the Target class, and select Add > Operation from the popup menu.
- Name the operation Request().
- Right click on Request, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Target class, and drag out Generalization > Class to create a subclass Adapter.
- Adapter will inherits the operations from Target. Right click on Adapter and select Related Elements > Realize all Interfaces from the popup menu.
- Move the mouse cursor over the Adapter class, and drag out Association > Class to create an associated class Adaptee.
- In practice, there may be multiple requests. To represent this, stereotype the class Target as PTN Members Creatable. Right click on Target and select Stereotypes > Stereotypes... from the popup menu.
In the Stereotype tab of the Class Specification dialog box, select PTN Members Creatable and click > to assign it to Target class. Click OK to confirm.
Up to now, the diagram should look like this:
- Select all classes on the class diagram.
- Right click on the selection and select Define Design Pattern... from the popup menu.
- In the Define Design Pattern dialog box, specify the pattern name Adapter. 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 adapter pattern to wrap a legacy Shape class.
- Create a new project Diagram Editor.
- Create a class diagram Domain Model.
- Right click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
- In the Design Pattern dialog box, select Adapter from the list of patterns.
- Click on Target in the overview.
- Rename Target to Shape, and operation Request to getColor at the bottom pane.
- Besides the operation getColor, we also need two more operations for getPosition and draw. Keep Target selected, click on the + button at the bottom pane, and select New Operation... from the popup menu.
- In the Operation Specification dialog box, name the operation getPosition. Check Abstract at the bottom of dialog box.
- Repeat steps 7 and 8 to create operation draw.
- Select Adapter in overview, and rename it as NewShape at the bottom pane. Rename also the operation Request to getColor. Note that if the option Auto Rename is on, rename of operation is not needed as this will be done automatically.
- Select Adaptee in overview, and rename it as LegacyShape at the bottom pane. Click OK to apply the pattern to diagram.
- We need to create the specific requests in LegacyShape. Select the operations in NewShape.
Press on the Ctrl key, and drag to LegacyShape to copy them.
This is the result: