Summary: The Intepreter Design Pettern is the basis for recursive algorithms on composite structures
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 Composite design pattern structures.
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.
| Interpreter Design Pattern UML Diagram |
|---|
![]() |
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.
| Interpreter Pattern Example |
|---|
![]() |
"Recursive algorithms on composite structures "