Skip to content Skip to navigation

Connexions

You are here: Home » Content » Interpreter Design Pettern

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

In these lenses

  • Design Patterns display tagshide tags

    This module is included inLens: Design Patterns
    By: Stephen Wong

    Comments:

    "Recursive algorithms on composite structures "

    Click the "Design Patterns" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Interpreter Design Pettern

Module by: Stephen Wong. E-mail the author

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.

Figure 1: UML diagram of interpreter pattern.
Interpreter Design Pattern UML Diagram
Interpreter Design Pattern UML Diagram
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.

Example 1

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 2: UML diagram of intepreter pattern demo.
Interpreter Pattern Example
erpreter Pattern Example
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.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks