Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 14, 2009
  • Views: 27,124
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Interpreter.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Client.
    create client class
  4. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class Context.
    create context class
  5. Move the mouse cursor over the Client class, and drag out Association > Class to create an associated class AbstractExpression.
    create abstract expression
  6. Right-click on AbstractExpression, and select Model Element Properties > Abstract to set it as abstract.
    set abstract expression abstract
  7. Right-click on AbstractExpression class, and select Add > Operation from the popup menu.
    add oper to abstract expression
  8. Name the operation Interpret(Context).
  9. Right-click on Interpret(Context), and select Model Element Properties > Abstract to set it as abstract.
    set oper abstract
  10. 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.
    sub classes added
  11. 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.
    realize interface
  12. Move the mouse cursor over the NonterminalExpression class, and drag out Aggregation > Class to AbstractExpression.
    associate nonterminalexp to absexpre
  13. 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.
    stereotype abstract expression
  14. 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.
    select members creatable
    Up to now, the diagram should look like this:
    ptn members creatable assigned
  15. In practice, there may be multiple TerminalExpression and/or NonterminalExpression. To represent this, assign them with PTN Cloneable, by following steps 13 and 14.
    ptn cloneable assigned

Defining Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Interpreter. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

In this section, we are going to apply the interpreter pattern to model the interpretation of expression.

  1. Create a new project Expression.
  2. Create a class diagram Expression.
  3. Right-click on the class diagram and sleect Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Interpreter from the list of patterns.
    select interpreter
  5. Click on AbstractExpression in the overview.
    select abstract expression
  6. Rename AbstractExpression to RegularExpression.
    rename abstract expression
  7. Select TerminalExpression in overview, and rename it to LiteralExpression at the bottom pane.
    rename terminal expression
  8. Select NonterminalExpression in overview, and rename it as SequenceExpression at the bottom pane. Click OK to apply the pattern to diagram.
    rename nonterminal expression
    This is the result:
    result



创造美好 共同成长

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK