Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Proxy.
- 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 Subject.
- Right click on Subject, and select Model Element Properties > Abstract to set it as abstract.
- Right click on the Subject 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 Subject class, and drag out Generalization > Class to create a subclass RealSubject.
- Repeat the previous step to create another subclass from Subject, namely Proxy.
- In practice, there may be multiple requests. To represent this, stereotype the class Subject as PTN Members Creatable. Right click on Subject and select Stereotypes > Stereotypes... from the popup menu.
In the Stereotypes tab of the Class Specification dialog box, select PTN Members Creatable and click > to assign it to Subject 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 Proxy. Keep the file name as it. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the proxy pattern in modeling a client class that talks to a proxy account class.
- Create a new project Account Management.
- 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 Proxy from the list of patterns.
- Click on Subject in the overview.
- Rename Subject to Account, and operation Request to Create at the bottom pane.
- Besides the operation Create, we also need two more operations for Suspend and Delete. Keep Subject 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 Suspend.
- Repeat steps 7 and 8 to create operation Delete.
- Select RealSubject in overview, and rename it as RealAccount at the bottom pane.
- Select Proxy in overview, and rename it as AccountProxy at the bottom pane. Click OK to apply the pattern to diagram.
We need to make the real object and the proxy class inherit operations from the subject class. Right click on RealAccount and AccountProxy, and select Related Elements > Realize all Interfaces from the popup menu.
The diagram should look like: