Interpreter Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) interpreter design pattern. By reading this tutorial, you will know how to develop a model for the interpreter 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 Interpreter.

- Select Class from the diagram toolbar. Click on the diagram to create a class. Name it 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 the 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 the 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 a 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 a Design Pattern on a Class Diagram
In this section, we are going to apply the interpreter pattern to model the interpretation of an expression.
- Create a new project Expression.
- Create a class diagram Expression.
- Right-click on the class diagram and select 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 the overview, and rename it to LiteralExpression at the bottom pane.

-
Select NonterminalExpression in the overview, and rename it as SequenceExpression at the bottom pane. Click OK to apply the pattern to the diagram.
This is the result: