Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Interpreter.
- 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 Context.
- Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class AbstractExpression.
- Right-click on AbstractExpression, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on AbstractExpression class, and select Add > Operation from the popup menu.
- Name the operation Interpret(Context).
- Right-click on Interpret(Context), and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the AbstractExpression class, and drag out Generalization > Class to create a subclass TerminalExpression. Repeat this step to create another subclass NonterminalExpression, from AbstractExpression.
- TerminalExpression and NonterminalExpression will inherit the operations from AbstractExpression. Select TerminalExpression and NonterminalExpression, right-click on them and select Related Elements > Realize all Interfaces from the popup menu.
- Move the mouse cursor over the NonterminalExpression class, and drag out Aggregation > Class to AbstractExpression.
- In practice, there may be multiple operations in AbstractExpression. To represent this, stereotype the class AbstractExpression as PTN Members Creatable. Right-click on AbstractExpression 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 AbstractExpression class. Click OK to confirm.
Up to now, the diagram should look like this:
- In practice, there may be multiple TerminalExpression and/or NonterminalExpression. To represent this, assign them with PTN Cloneable, by following steps 13 and 14.
Defining Pattern
- 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 Interpreter. 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 interpreter pattern to model the interpretation of expression.
- Create a new project Expression.
- Create a class diagram Expression.
- Right-click on the class diagram and sleect Utilities > Apply Design Pattern... from the popup menu.
- In the Design Pattern dialog box, select Interpreter from the list of patterns.
- Click on AbstractExpression in the overview.
- Rename AbstractExpression to RegularExpression.
- Select TerminalExpression in overview, and rename it to LiteralExpression at the bottom pane.
-
Select NonterminalExpression in overview, and rename it as SequenceExpression at the bottom pane. Click OK to apply the pattern to diagram.
This is the result: