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.
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" />
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.
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.
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 ...