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 Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions 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 Connexions 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

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

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

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

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

| A lens (?)

Definition of a lens

Lenses

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

What is in a lens?

Lens makers point to Connexions 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 Connexions 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