O'Reilly logo

C# 3.0 Design Patterns by Judith Bishop

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Interpreter Pattern

The Interpreter pattern supports the interpretation of instructions written in a language or notation defined for a specific purpose. The notation is precise and can be defined in terms of a grammar.

Illustration

XML (eXtended Markup Language) is a very popular way of expressing the format of data. XML consists of tags that introduce attributes and associated values in a simple nested and sequential notation. Consider the example in Figure 10-4—(a) shows an XML description of a GUI depicted in (b). The XML corresponds to the controls and parameters of a Windows form. Thus, the following:

<TextBox Top="10" Left="100" Name="eurobox" />

specifies a TextBox control followed by pixel values for its top-left corner and the name by which it will be known in the program ("eurobox"). The corresponding label at top=10 pixels has the text "Paid on hols" so that will be opposite the eurobox control when it is laid out on the form.

Interpreter pattern illustration—XML description of a GUI

Figure 10-4. Interpreter pattern illustration—XML description of a GUI

The XML specification is read into a program, checked, and interpreted into GUI objects to be displayed on the screen. Thus, this example illustrates the essence of the Interpreter pattern.

Grammars

Languages can be expressed in other notations as well. A familiar form is a grammar of terms. Terms written in sequence must follow each other; alternatives are indicated with a | and repetition ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required