<?xml version="1.0" encoding="utf-8"?>
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:q="http://cnx.rice.edu/qml/1.0" id="new" module-id="" cnxml-version="0.6">
  <title>Interpreter Design Pettern</title>
  <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4">
  <!-- WARNING! The 'metadata' section is read only. Do not edit below.
       Changes to the metadata section in the source will not be saved. -->
  <md:content-id>m16877</md:content-id>
  <md:title>Interpreter Design Pettern</md:title>
  <md:version>1.2</md:version>
  <md:created>2008/06/23 15:42:21 GMT-5</md:created>
  <md:revised>2009/06/26 15:43:36.440 GMT-5</md:revised>
  <md:authorlist>
    <md:author id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:author>
  </md:authorlist>
  <md:maintainerlist>
    <md:maintainer id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  <md:license href="http://creativecommons.org/licenses/by/2.0/"/>
  <md:licensorlist>
    <md:licensor id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:licensor>
  </md:licensorlist>
  <md:keywordlist>
    <md:keyword>design</md:keyword>
    <md:keyword>Object</md:keyword>
    <md:keyword>oriented</md:keyword>
    <md:keyword>pattern</md:keyword>
    <md:keyword>programming</md:keyword>
  </md:keywordlist>
  <md:subjectlist>
    <md:subject>Science and Technology</md:subject>
  </md:subjectlist>
  <md:abstract>The Intepreter Design Pettern is the basis for recursive algorithms on composite structures</md:abstract>
  <md:language>en</md:language>
  <!-- WARNING! The 'metadata' section is read only. Do not edit above.
       Changes to the metadata section in the source will not be saved. -->
</metadata>
  <content>
    <para id="delete_me">The Interpreter design pattern is usually described in terms of interpreting grammars.  In this discussion, we will focus on a more general usage with regards to operations on <link url="http://cnx.org/content/m14795/latest/">Composite design pattern</link> structures.</para><para id="eip-559">The Interpreter pattern is very useful for adding functionality to Composite pattern structures.  Composites represent multi-part systems that are being accessed as single abstract entities (e.g. as AComponent below).    The Intepreter pattern requires an abstract method at the top level of the composite structure that is used by the component's client to process the entire structure. Concrete methods are thus implemented in the terminal and non-terminal components to do the actual processing. </para><para id="eip-569"><figure id="fig1"><title>Interpreter Design Pattern UML Diagram</title><media id="dogpic" alt="Interpreter Design Pattern UML Diagram">
		<image mime-type="image/png" src="interpreter.png"/>
	</media>
	
<caption>UML diagram of interpreter pattern.
  </caption></figure>

The abstract AComponent defines an abstract operation() to process the entire composite structure.   The concrete components define concrete operation()'s to process themselves.   The Composite class defines a concrete operation() as well, which in addition to any particular processing that it needs, must also recursively call operation() on all of its composees. 

</para><example id="eip-845"><para id="eip-12">Suppose we have the following binary tree structure that holds int values.    AIntTree represents the abstract tree.   Leaf is a leaf node and holds a single int, data.   InternalNode is an internal node with two branches, left and right, which are AIntTrees.  

<figure id="fig2"><title>Interpreter Pattern Example</title><media id="AIntTreepic" alt="erpreter Pattern Example">
		<image mime-type="image/png" src="AIntTree.png"/>
	</media>
	
<caption>UML diagram of intepreter pattern demo.
  </caption></figure>
        
To add the ability to sum the contents of the entire tree, an abstract sum() method is added to the AIntTree superclass.   A client that holds a reference to an AIntTree  object will call sum() on that object.  Concrete sum() methods are added to both the Leaf and InternalNode sub-classes.   Leaf.sum() simply needs to return the value of data.  InternalNode.sum() simply returns the sum of the left and rightsums.
</para>
</example>   
  </content>
  
</document>
