This document discusses the Interpreter and Iterator patterns from the Gang of Four (GoF) patterns. [1] The Interpreter pattern represents grammars and expressions as object structures to define a language and interpret sentences in that language. [2] The Iterator pattern allows iterating over elements of an object sequentially without exposing its underlying representation. [3] Both patterns have limitations and the document discusses when and how they could be improved.
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Hw6 interpreter iterator GoF
1. UTAH STATE UNIVERSITY
COMPUTER SCIENCE
CS-7350 Reading Assignment
GoF Patterns: Interpreter, Iterator
Jorge Edison Lascano
Spring 2012
01-27-2012
INTERPRETER, ITERATORGoFPATTERNS
Interpreter, its main intent is interpreting well-structured sentences based on grammars, looking
for sequence of actions that make sense syntactically. One constraint is that it only serves for
simple grammars, then the question is How to measure the complexity of the grammar?. Other
difficulty is the representation of the tree structure (solved in certain way by composite). The
Interpret method represents the main behavior, but it needs help to traverse the structure.
Concluding, this pattern by itself does not solve all the proposed problems by GoF.
The consideration of a complex grammar is still present. It is said that it is helpful for regular
expressions, but not for parsers. So, the complexity could be measured by the level of
(de)composition, or the complexity in the grammatical structure of the sentences.
Iterator, also known as cursor, it´s intent is to allow access to elements sequentially without
exposing their representation. The main use is mentioned as data structure traverse, but, this
feature is already implemented by almost every modern language, namely:Java, C#, Smalltalk in
their Collections. Now, the question is: Is it really necessary to implement an iterator? And the
answer is YES, if we need to implement a different way (than traditional data structures) to
traverse the elements and also if we need to implement multiple traversal ways at the same time.
As this is a behavior pattern, the main concern should be oriented to decide the place where the
traversal algorithm should be implemented. Maybe using a Bridge between the ConcreteIterator
and the ConcreteAggregate could solve this problem.